How to get the specific line of code that threw an error

五迷三道 提交于 2019-12-10 13:50:15

问题


I'm getting a java.lang.NullPointerException right when the app launches and it shuts down. The error from the emulator is "Unfortunately, appname has stopped". It was working fine, until I wrote a bunch of new code, and changed the manifest. Hopefully it's not the manifest, but my question is, how can I find out what line of code is the problem? The trace dump means nothing to me, and even though it's verbose, having ...11 more doesn't let me see the whole thing.

I don't really know what that error means. I've searched for it, but there seems to be a list of things it could mean. I've tried Project>Clean, I've tried messing with the manifest again, but I still get the error. I've checked/unchecked external libraries. Just done what people have suggested to do for other people getting the same error. So I'd really like to know, what line set it off?

Here is the output if this helps:

06-29 08:37:23.680: E/AndroidRuntime(1225): FATAL EXCEPTION: main
06-29 08:37:23.680: E/AndroidRuntime(1225): java.lang.RuntimeException: Unable to    instantiate activity      ComponentInfo{com.upliftly.android/com.upliftly.android.UpliftlyActivity}: java.lang.NullPointerException
06-29 08:37:23.680: E/AndroidRuntime(1225):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1879)
06-29 08:37:23.680: E/AndroidRuntime(1225):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
06-29 08:37:23.680: E/AndroidRuntime(1225):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
06-29 08:37:23.680: E/AndroidRuntime(1225):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
06-29 08:37:23.680: E/AndroidRuntime(1225):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-29 08:37:23.680: E/AndroidRuntime(1225):     at android.os.Looper.loop(Looper.java:137)
06-29 08:37:23.680: E/AndroidRuntime(1225):     at android.app.ActivityThread.main(ActivityThread.java:4340)
06-29 08:37:23.680: E/AndroidRuntime(1225):     at java.lang.reflect.Method.invokeNative(Native Method)
06-29 08:37:23.680: E/AndroidRuntime(1225):     at java.lang.reflect.Method.invoke(Method.java:511)
06-29 08:37:23.680: E/AndroidRuntime(1225):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-29 08:37:23.680: E/AndroidRuntime(1225):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-29 08:37:23.680: E/AndroidRuntime(1225):     at dalvik.system.NativeStart.main(Native Method)
06-29 08:37:23.680: E/AndroidRuntime(1225): Caused by: java.lang.NullPointerException
06-29 08:37:23.680: E/AndroidRuntime(1225):     at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
06-29 08:37:23.680: E/AndroidRuntime(1225):     at com.upliftly.android.UpliftlyActivity.<init>(UpliftlyActivity.java:19)
06-29 08:37:23.680: E/AndroidRuntime(1225):     at java.lang.Class.newInstanceImpl(Native Method)
06-29 08:37:23.680: E/AndroidRuntime(1225):     at java.lang.Class.newInstance(Class.java:1319)
06-29 08:37:23.680: E/AndroidRuntime(1225):     at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
06-29 08:37:23.680: E/AndroidRuntime(1225):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)
06-29 08:37:23.680: E/AndroidRuntime(1225):     ... 11 more

回答1:


Usually when you see a stack trace like the one you posted, you should focus in the lines after the last

Caused by: 

line. After that, detect the line that is has your package name and it is (in most cases) that line that caused the exception from your code. In the stack trace that you posted, that line is

at com.upliftly.android.UpliftlyActivity.<init>(UpliftlyActivity.java:19)



回答2:


Look for your app's package name (I'm guessing it is com.uplifty.android):

at com.upliftly.android.UpliftlyActivity.<init>(UpliftlyActivity.java:19)



回答3:


Like two of the answers said: look for your package name like this example:

at com.upliftly.android.UpliftlyActivity.<init>(UpliftlyActivity.java:19)

The parenthesis tells you in which Java file and what line the error occurs on. So in this case UpliftlyActivity.java line 19.

Though error in you Manifest or in the xml files won't show you a file or line number where the error is, so they are trickier to find.




回答4:


In the logcat it'll give the package name and your activity name with line number .Then you need to observe that particular line in that what are the variables available then you need to check that variables and its corresponding xml id's. so with the help of this you can avoid this error. i think in your posted logcat

at com.upliftly.android.UpliftlyActivity.<init>(UpliftlyActivity.java:19)

the above line seems to be the your activity line in that you can check and avoid the error.



来源:https://stackoverflow.com/questions/11262343/how-to-get-the-specific-line-of-code-that-threw-an-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!