質問

I don't know how to figure out where an uncaught exception is coming from. I have looked through the stackoverflow posts and tried looking for several of the solutions. As far as I can tell, I have my activity, "Monitor", listed in my manifest and it is part of the package. But I get a null pointer exception when I start the "Monitor" activity. What should I be looking for to find this exception?

02-03 21:44:14.192: E/Trace(19701): error opening trace file: No such file or directory (2)
02-03 21:44:14.262: I/BugSenseHandler(19701): Registering default exceptions handler
02-03 21:44:14.633: I/Setup(19701): Setup activity created
02-03 21:44:14.813: I/BugSenseHandler(19701): Flushing...
02-03 21:44:14.823: I/BugSenseHandler(19701): Registering default exceptions handler
02-03 21:44:15.023: W/BugSenseHandler(19701): Transmitting ping Exception No peer certificate
02-03 21:44:15.403: I/Adreno200-EGLSUB(19701): <ConfigWindowMatch:2087>: Format RGBA_8888.
02-03 21:44:15.594: E/(19701): <s3dReadConfigFile:75>: Can't open file for reading
02-03 21:44:15.594: E/(19701): <s3dReadConfigFile:75>: Can't open file for reading
02-03 21:44:19.318: W/dalvikvm(19701): threadid=1: thread exiting with uncaught exception (group=0x41bfb438)
02-03 21:44:22.401: E/AndroidRuntime(19701): FATAL EXCEPTION: main
02-03 21:44:22.401: E/AndroidRuntime(19701): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mpeterson.sousvide/com.mpeterson.sousvide.Monitor}: java.lang.NullPointerException
02-03 21:44:22.401: E/AndroidRuntime(19701):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2024)
02-03 21:44:22.401: E/AndroidRuntime(19701):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2132)
02-03 21:44:22.401: E/AndroidRuntime(19701):    at android.app.ActivityThread.access$600(ActivityThread.java:139)
02-03 21:44:22.401: E/AndroidRuntime(19701):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1231)
02-03 21:44:22.401: E/AndroidRuntime(19701):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 21:44:22.401: E/AndroidRuntime(19701):    at android.os.Looper.loop(Looper.java:137)
02-03 21:44:22.401: E/AndroidRuntime(19701):    at android.app.ActivityThread.main(ActivityThread.java:5021)
02-03 21:44:22.401: E/AndroidRuntime(19701):    at java.lang.reflect.Method.invokeNative(Native Method)
02-03 21:44:22.401: E/AndroidRuntime(19701):    at java.lang.reflect.Method.invoke(Method.java:511)
02-03 21:44:22.401: E/AndroidRuntime(19701):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
02-03 21:44:22.401: E/AndroidRuntime(19701):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
02-03 21:44:22.401: E/AndroidRuntime(19701):    at dalvik.system.NativeStart.main(Native Method)
02-03 21:44:22.401: E/AndroidRuntime(19701): Caused by: java.lang.NullPointerException
02-03 21:44:22.401: E/AndroidRuntime(19701):    at android.app.Activity.findViewById(Activity.java:1851)
02-03 21:44:22.401: E/AndroidRuntime(19701):    at com.mpeterson.sousvide.Monitor.<init>(Monitor.java:51)
02-03 21:44:22.401: E/AndroidRuntime(19701):    at java.lang.Class.newInstanceImpl(Native Method)
02-03 21:44:22.401: E/AndroidRuntime(19701):    at java.lang.Class.newInstance(Class.java:1319)
02-03 21:44:22.401: E/AndroidRuntime(19701):    at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
02-03 21:44:22.401: E/AndroidRuntime(19701):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2015)
02-03 21:44:22.401: E/AndroidRuntime(19701):    ... 11 more
役に立ちましたか?

解決

Provided that you have editTextCurTemp defined as TextView in your_layout_file.xml, you must call

setContentView(R.layout.your_layout_file) 

BEFORE you do any findViewById(), otherwise you will get NPE.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top