How do I prevent exception catching in Android?
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, and if a line of mine causes an exception, then rather than stopping on that line and highlighting it, it throws me into the ActivityThread class's code, which apparently I don't have, so I just get a "Source not found" screen.
Trying to find the troublesome line like this is very frustrating, so I'm trying to find a way to prevent Android's code from catching every exception during development. My searches online have yielded no information as to how I go about doing this, so I decided to ask here.
Here is the stack trace before the exception is thrown in my code:
Thread [<1> main] (Suspended (breakpoint at line 72 in GameView))
GameView.showMen开发者_高级运维u() line: 72 GameView.init() line: 59 GameView.(Context, AttributeSet) line: 51 Constructor.constructNative(Object[], Class, Class[], int, boolean) line: not available [native method] Constructor.newInstance(Object...) line: 415 PhoneLayoutInflater(LayoutInflater).createView(String, String, AttributeSet) line: 505 PhoneLayoutInflater(LayoutInflater).createViewFromTag(String, AttributeSet) line: 570 PhoneLayoutInflater(LayoutInflater).rInflate(XmlPullParser, View, AttributeSet) line: 623 PhoneLayoutInflater(LayoutInflater).inflate(XmlPullParser, ViewGroup, boolean) line: 408 PhoneLayoutInflater(LayoutInflater).inflate(int, ViewGroup, boolean) line: 320 PhoneLayoutInflater(LayoutInflater).inflate(int, ViewGroup) line: 276 PhoneWindow.setContentView(int) line: 207 MainActivity(Activity).setContentView(int) line: 1657 MainActivity.onCreate(Bundle) line: 20 Instrumentation.callActivityOnCreate(Activity, Bundle) line: 1047 ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1586 ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1638 ActivityThread.access$1500(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 117 ActivityThread$H.handleMessage(Message) line: 928 ActivityThread$H(Handler).dispatchMessage(Message) line: 99 Looper.loop() line: 123 ActivityThread.main(String[]) line: 3647 Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method] Method.invoke(Object, Object...) line: 507 ZygoteInit$MethodAndArgsCaller.run() line: 839 ZygoteInit.main(String[]) line: 597 NativeStart.main(String[]) line: not available [native method]
and here is the stack trace after Eclipse has stopped execution on account of the exception:
Thread [<1> main] (Suspended (exception RuntimeException)) ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1622
ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1638 ActivityThread.access$1500(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 117 ActivityThread$H.handleMessage(Message) line: 928 ActivityThread$H(Handler).dispatchMessage(Message) line: 99 Looper.loop() line: 123 ActivityThread.main(String[]) line: 3647 Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method] Method.invoke(Object, Object...) line: 507 ZygoteInit$MethodAndArgsCaller.run() line: 839 ZygoteInit.main(String[]) line: 597 NativeStart.main(String[]) line: not available [native method]
Any help would be highly appreciated.
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.
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
}
}
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
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.
精彩评论