Eclipse Android Debugger - 我的代码在哪里引起异常? [英] Eclipse Android Debugger - Where in my code did I cause the exception?

查看:192
本文介绍了Eclipse Android Debugger - 我的代码在哪里引起异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

诚然,我的问题基本上与这个问题相同,但似乎没有得到答案:



handleStopActivity中的NullPointerException - 不引用堆栈跟踪中的代码



在上周内下载了Eclipse Helios,Android开发工具插件和JDK。我正在使用我的设备上的一个应用程序,在Debug模式下运行它,并且意外终止了。我意识到我造成了一个NullPointerException,问题本身并不是一个很长的问题。



然而,一个问题是调试器似乎无法识别哪里在我的代码中,异常被抛出。堆栈跟踪不引用我的代码。



的确,如果我将以下内容放在 OnCreate()方法中,我得到相同的问题

  public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// lI(onCreate()); //一个愚蠢的记录事情我搞砸了

整数iDareYou = null;
iDareYou.byteValue();

Eclipse肯定警告我,代码可能会在我脸上爆炸。但是当我实际在我的设备上运行它时,返回的堆栈跟踪如下:

 线程[< 1> main](Suspended(异常RuntimeException))
ActivityThread.performLaunchActivity(ActivityThread $ ActivityRecord,Intent)行:2787
ActivityThread.handleLaunchActivity(ActivityThread $ ActivityRecord,Intent)行:2803
ActivityThread.access $ 2300(ActivityThread,ActivityThread $ ActivityRecord,Intent)行:135
ActivityThread $ H.handleMessage(Message)行:2136
ActivityThread $ H(Handler).dispatchMessage(Message)行:99
Looper.loop()行:144
ActivityThread.main(String [])line:4937
Method.invokeNative(Object,Object [],Class,Class [],Class,int,boolean)line :不可用[native method]
Method.invoke(Object,Object ...)行:521
ZygoteInit $ MethodAndArgsCaller.run()行:868
ZygoteInit.main(String [] )line:626
NativeStart.main(String [])行:不可用[native method]



我搜索了一些答案,不幸的是,我可以想出来的是一些问题的为什么我的Android应用程序导致NullPointerException?



我希望找出我应该做的不同,以便能够在我的代码中找到引起异常的一点。因为我相信这个能力在将来的某个时刻会派上用场!






添加一些信息。作为下面提到的第一个评论,调试器暂停线程的状态不是应用程序的最终状态。按下恢复几次将流程沿着流程实际终止,尽管不幸的是Eclipse / ADT Debug窗口中没有显示有用的堆栈跟踪。 然而,在这一点上,我粘贴在 之后的堆栈跟踪,我的代码中NPE的堆栈跟踪的前3个独特的行被输出到LogCat。以前我曾经看过LogCat是否有任何可以使用的东西,但很可能是在调试器中按恢复之前,将进程转移到这个状态。不幸的是,这样做不会在我的代码中在实际的调试器中显示NPE的堆栈跟踪。



但是消息并没有坏 - 从变量窗口在右边,我可以看到在线程当前的范围内有一个NPE,所以至少会给我一个线索,NPE正在进行(当然还有LogCat)。



所以基于不同主题的建议,我发现最好的工作是为NullPointerException添加一个抓取和未捕获断点。当我这样做并重新启动时,调试器在我的代码中显示NPE的堆栈跟踪,并将编辑窗口直接移动到我导致NPE的行。



目前我相信这是因为NPE被Android框架所捕获(可能再次被抛出)。 似乎原来的困境是由于Eclipse / ADT / DalvikVM调试堆栈跟踪窗口不包括由...引起的信息(据我所知),这是在堆栈跟踪中显示的 出现在LogCat 中。我会进一步调查这是否可以补救:)



我欣赏Bert F的建议,因为我不完全知道该线程尚未成为最终'状态。虽然在LogCat中堆栈跟踪是[稍后]显示的,但是我没有在LogCat中错过它,因为它还没有,但我错过的是恢复,这将导致堆栈跟踪在LogCat中输出。



即使我最初让调试器在我的代码中使用断点停止,有一个事实,我可以最终获得堆栈跟踪在恢复线程后的LogCat中,这样就有效地解决了这个问题。我唯一可以要求的其他事情是让调试器停在我的代码行中,导致异常,而不是被抓到,后来被Android框架重新抛出。但是我相信这将是一个不同的问题:)如果我点击在LogCat窗口中显示的堆栈跟踪中的相应行,Eclipse将会很有用。

解决方案

我运行了你的确切代码,LogCat给了我以下例外:

 导致:java.lang.NullPointerException 
at mypackage.test.TestActivity.onCreate(TestActivity.java:23)

当然这是



iDareYou.byteValue(); p>

语句。这显然是一个光明的一天!



我建议您必须学习如何使用 Dalvik Debug Monitor Server 以正确的方式。



日志你发布似乎很无用,你是否搞砸了一些LogCat过滤器(发生在我身上)一次?



OFFTOPIC:我的代码在哪里我导致异常? - 我认为你的问题没有制定好。



我认为这个问题的正确名称可能是 - 如何使用Android调试器和LogCat?一些例外没有正确记录!'


Admittedly, my question is basically the same as this one, but it seems to have been left unanswered:

NullPointerException in handleStopActivity -- No reference to my code in stack trace

Downloaded Eclipse Helios, Android Developer Tools Plugin, and the JDK all within the last week. I was messing around with an app on my device, ran it in Debug mode, and it unexpectedly terminated. I realized I had caused a NullPointerException and the problem itself was not an issue for very long.

What is an issue, however, is that the debugger seems unable to identify where in my code the exception was thrown. The stack trace does not reference my code.

Indeed, if I put the following in the OnCreate() method, I get the same problem

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //lI("onCreate()");  //A silly logging thing I messed around with

    Integer iDareYou = null;
    iDareYou.byteValue();

Much to its credit, Eclipse certainly warns me that the code is likely to blow up in my face. But when I actually run this on my device, the stack trace returned is the following:

Thread [<1> main] (Suspended (exception RuntimeException))  
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2787  
ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2803   
ActivityThread.access$2300(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 135 
ActivityThread$H.handleMessage(Message) line: 2136  
ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
Looper.loop() line: 144 
ActivityThread.main(String[]) line: 4937    
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
Method.invoke(Object, Object...) line: 521  
ZygoteInit$MethodAndArgsCaller.run() line: 868  
ZygoteInit.main(String[]) line: 626 
NativeStart.main(String[]) line: not available [native method]  

I searched around for some answers and unfortunately all I could come up with were questions of the 'Why is my Android app causing a NullPointerException'? variety.

I'm hoping to find out what I should be doing differently in order to be able to find the point in my code where I caused the exception.... because I'm sure this ability would come in handy at some point in the future!


Going to edit this to add some information. As the first comment below mentions, the state in which the debugger suspends the thread is not the 'final' state of the application. Pressing 'resume' a couple times moves the process along to the point where the process actually terminates, although unfortunately no helpful stack trace is displayed in the Eclipse/ADT Debug window. However, at this point the stack trace I pasted above Followed By the first 3 unique lines of the stack trace of the NPE in my code is output to LogCat. I had previously looked to see if LogCat had anything I could use, but most likely it was before I had pressed 'resume' in the debugger to move the process to this state. Unfortunately though, doing it this way never displays a stack trace of the NPE in my code in the actual debugger.

But the news isnt all bad - from the information in the 'Variables' window on the right, I can see that there is an NPE in the thread's current scope, so that would at least give me a clue that an NPE is going on (and of course there is LogCat).

So based on a suggestion in a different topic, the best work around I've found is to add a "Caught and Uncaught" breakpoint for NullPointerException. When I do this and relaunch, the debugger displays the stack trace of the NPE in my code, and moves the edit window straight to the line where I caused the NPE.

At the moment I believe this is because the NPE is being caught (and presumably, thrown again) by the Android framework. It appears the original dilemma was caused by the fact that the Eclipse/ADT/DalvikVM Debug Stack Trace window does not include the 'caused by' information (as far as I can tell) that is displayed in the stack trace "later" appearing in LogCat. I will investigate further as to whether this can be remedied :)

I appreciate Bert F's suggestion, as I was not fully aware that the thread was not yet in its 'final' state. While it is true that the stack trace is [later] displayed in LogCat, I did not 'miss' it in LogCat because it was not [yet] there, what I did miss was hitting 'resume' which would have caused the stack trace to be output in LogCat.

Even though I originally got the debugger to stop at the line in my code using a breakpoint, there is a case for the fact that I could have ultimately obtained the stack trace in LogCat after resuming the thread, which would have effectively solved the problem. The only other thing I could really ask for would be to have the debugger stop at the line in my code causing the exception, instead of it being caught and later re-thrown by the Android framework. But I believe that would be a different question :) And Eclipse, being useful as it is, will send me there if I click on the appropriate line in the stack trace displayed in the LogCat window.

解决方案

I ran your exact code and LogCat gave me the following exceptions:

Caused by: java.lang.NullPointerException
    at mypackage.test.TestActivity.onCreate(TestActivity.java:23)

Of course it is the

iDareYou.byteValue();

statement. This is clear as a bright day!

I suggest that you must learn how to use Dalvik Debug Monitor Server in the proper way.

The log you posted seems pretty useless, did you messed up some of the LogCat filters(it happened to me once)?

OFFTOPIC: "Where in my code did I cause the exception?" - I think that your question is not well formulated.

I think that proper name of this question might be - 'How to use Android debugger and LogCat? Some exceptions are not logged properly!'

这篇关于Eclipse Android Debugger - 我的代码在哪里引起异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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