如何在移动设备中获取堆栈跟踪? [英] How to get the stacktrace in a mobile device?

查看:167
本文介绍了如何在移动设备中获取堆栈跟踪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想知道导致这个例外的原因。



设备显示:


NullPointerException java / lang / NullPointerException


此错误仅发生在设备中,在仿真器中运行,应用程序正常运行。



我使用 microlog 来调试我的应用程序。但是如果日志被启用,应用程序可以正常工作。



当我获得这个NullPointerException时,有没有办法获取堆栈跟踪?我不需要所有的细节,就像这个方法导致这个例外的那样。



更新:我在另一个诺基亚安装了相同的应用程序S40并没有发生相同的错误。




  • 诺基亚2660 - 错误

  • 诺基亚6131 - 无错误



更新2:不知何故,我发现是什么导致了NullPointerException。

  import javax.microedition.lcdui.Canvas; 
import javax.microedition.lcdui.Graphics;

public class OuterClass extends Canvas {

private Config config;

public OuterClass(){
this.config = new Config();
}

public void paint(Graphics graphics){
HelperClass helper = new HelperClass(this.config);
helper.doStuff();
}

public void dispose(){
this.config = null;
}

public class配置实现IConfig {
public int getSomething(){
// ...
}
}
}


public class HelperClass {

private IConfig config;

public HelperClass(IConfig){
this.config = config;
}

public doStuff(){
config.getSomething(); //这里抛出NullPointerException
}
}

在某些情况下,被启动,并在导致NPE的helper.doStuff()之前调用OuterClass.dispose()。我认为当我启用日志时,它使线程更慢,并且当我希望被调用时调用helper.doStuff()。

解决方案



在Series40上调试JavaME应用程序的通常的强力方式是,您不会在Nokia Series40手机上找到任何方法来保存Throwable堆栈跟踪。修改你的代码以在内存中创建一个堆栈跟踪。



我在说的是:




  • 您可以识别的每个线程(包括系统回调线程)都需要自己的包含字符串的Stack对象。显然,这会增加你的应用程序的内存占用,但保留在内存中应该限制对竞争条件的影响。


  • 当你的代码进入一个方法时,将方法签名添加到当前线程堆栈。当该方法退出(并且您最好只有一个方法一个退出点)它会弹出堆栈的顶部。


  • 您可以添加附加的调试信息堆栈,像代码的不同位置的变量值。


  • 您不一定需要将其添加到代码中的每个方法。 p>


  • 您可以将 try {} catch(Throwable){} 添加到您确定的每个线程的切入点并将堆栈转储到文件或屏幕上(在表单中)。




显然,这不是您想在大型现有代码库中的很多地方手动添加的更改类型。然而,您可以将其作为未来组织编码标准的一部分,并编写源代码解析脚本以自动将其添加到现有代码中。


I'm getting a NullPointerException in a Nokia S40.

I want to know what is causing this exception.

The device shows:

NullPointerException java/lang/NullPointerException

This error only occurs in the device, running in the emulator the application works fine.

I use microlog to debug my application. But the application works fine if the log is enabled.

Is there a way to get the stack trace when I get this NullPointerException? I don't need all details like the line number just which method cause this exception.

UPDATE: I installed the same application in another Nokia S40 and the same error didn't occur.

  • Nokia 2660 - error
  • Nokia 6131 - no error

UPDATE 2: Somehow I find what was causing the NullPointerException.

    import javax.microedition.lcdui.Canvas;
    import javax.microedition.lcdui.Graphics;

    public class OuterClass extends Canvas {

    private Config config;

    public OuterClass() {
        this.config = new Config();
    }

    public void paint(Graphics graphics) {
        HelperClass helper = new HelperClass(this.config);
        helper.doStuff();
    }

    public void dispose() {
        this.config = null;
    }

    public class Config implements IConfig {
        public int getSomething() {
            // ...
        }
    }
}


 public class HelperClass {

        private IConfig config;

        public HelperClass(IConfig) {
            this.config = config;
        }

        public doStuff() {
            config.getSomething(); // Here is thrown NullPointerException
        }
    }

In some situations a thread is started and call the OuterClass.dispose() before the helper.doStuff() causing the NPE. I think when I enabled the log it made the thread slower and helper.doStuff() was called when I expected it to be called.

解决方案

You are not going to find any way to save a Throwable stack trace on a Nokia Series40 handset.

The usual brute force way of debugging JavaME application on Series40 is to modify your code to create a stack trace yourself in memory.

What I'm talking about is:

  • Each Thread that you can identify (including system callback threads) needs its own Stack object, containing strings. Obviously, this increases the memory footprint of your application somewhat but keeping it in memory should limit the impact on race conditions.

  • When your code enters a method, it adds the method signature to the current Thread Stack. When the method exits (and you better only have one exit point per method) it pops the top of the Stack.

  • You can add aditional debug info on the stack, like values of variables in different places of the code.

  • You don't necessarily need to add this to every single method in your code.

  • You can add try{}catch(Throwable){} to the entry point of every thread you identified and either dump the stack in a file or on the screen (in a Form).

Obviously, this is not the kind of change you want to manually add in a lot of places in a large existing codebase. You can however make it part of your organisation coding standards for the future and write a source code parsing script to automatically add it to existing code.

这篇关于如何在移动设备中获取堆栈跟踪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆