How do I prevent exception catching in Android?

前端 未结 4 586
北荒
北荒 2021-01-22 17:50

I\'m trying to develop an application for Android, but I\'m having difficulties tracing the source and cause of each exception I get in the process. My code runs in an Activity,

相关标签:
4条回答
  • 2021-01-22 18:31

    The first line in the Stack trace shows you where its blowing up. In your case:

    Thread [<1> main] (Suspended (breakpoint at line 72 in GameView))
    GameView.showMenu() line: 72
    GameView.init() line: 59
    GameView.(Context, AttributeSet) line: 51
    
    0 讨论(0)
  • 2021-01-22 18:33

    When the debugger breaks like that, just continue execution (probably you'll need to do this 2 or 3 times). Then look at the LogCat output for a meaningful stack trace.

    0 讨论(0)
  • 2021-01-22 18:39

    Sounds to me like you should be wrapping your code in question in a try/catch block so you can (A) gracefully handle the exception and (B) Be able to set a breakpoint in the catch block and inspect your variables while debugging. Or am I misunderstanding?

    edit:

    As an example, if you have an Activity and you're in your onCreate (my Android-fu is a little rusty) and it's

    public void onCreate(Bundle blahblah) {
      My code here
    }
    

    you would instead do

    public void onCreate(Bundle blahblah) {
      try { 
        My code here
      } catch (Exception e) {
        Log.d(Do something to print your stacktrace here); <-- Set your breakpoint here
      }
    }
    
    0 讨论(0)
  • 2021-01-22 18:47

    To me, that output doesn't look like it is actually the stacktrace of the exception. That just looks like the Thread information from the debug perspective. Try using logcat in either the ddms perspective or the ddms standalone tool to look at the actual exception that is thrown.

    0 讨论(0)
提交回复
热议问题