问题
This is a bit of a shot in the dark, but perhaps I'm making an obvious mistake; I have a LinearLayout defined in my main.xml, with an id "@+id/imageListContainer". There is nothing else in my main.xml, and the LinearLayout is completely plain (As eclipse inserts it for you from the GUI editor).
<LinearLayout
android:id="@+id/imageListContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
I then want to populate the LinearLayout dynamically, using this code:
LinearLayout imageContainer = (LinearLayout)findViewById(R.id.imageListContainer);
from withhin the applications onCreate() callback function.
This used to work fine. However, as I started to add bits and pieces of code here and there (mainly in other activities), at some point it stopped working, and now I consistently get returned a nullpointer by findViewById(). I tried re-generating R.java, renaming the LinearLayout (to make sure I had control from where it was accessed; but it's only accessed from this one single place). I tried putting the code into onStart() instead, thinking that onCreate might run too early, creating a race-condition, but no luck with that either. I also made sure that the LinearLayout is indeed called "imageListContainer", which it is (otherwise the generated R.java would be wrong and javac would complain anyway) and that there is no duplicate "imageListContainer" object anywhere.
I'm pretty much lost as to what the cause of this could be, so I'd appreciate any suggestions or guesses.
Here is the stacktrace:
E/AndroidRuntime( 327): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime( 327): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.PictureListView/com.company.PictureListView.PictureListView.PictureListActivity}: java.lang.NullPointerException
E/AndroidRuntime( 327): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
E/AndroidRuntime( 327): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
E/AndroidRuntime( 327): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
E/AndroidRuntime( 327): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
E/AndroidRuntime( 327): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 327): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 327): at android.app.ActivityThread.main(ActivityThread.java:4363)
E/AndroidRuntime( 327): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 327): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 327): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime( 327): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
E/AndroidRuntime( 327): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 327): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 327): at com.company.PictureListView.PictureListActivity.updateMainActivityScreen(PictureListActivity.java:50)
E/AndroidRuntime( 327): at com.company.PictureListPictureListActivity.onCreate(PictureListActivity.java:38)
E/AndroidRuntime( 327): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime( 327): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
PS: I forgot to mention, I also called setContentView(R.layout.main), so that's not the problem either.
PSS: I tried adding a button to the GUI instead, and then calling the code containing the findViewById() inside the callback for that button, and that works! This leads me back to the idea that there might be some race-condition going on.
回答1:
This was the answer from the comments of the question:
Do you have an inner class in your activity? I read the exception trace carefully: first com.company.PictureListPictureListActivity.onCreate then com.company.PictureListView.PictureListActivity.updateMainActivityScreen --> seems to me findViewById is called in a different object. Try moving the findViewByID into the onCreate and check if it works there.
It seems there was an inner class at some point of development, which was later removed. Somehow project files were not updated appropriately. Cleaning the project, refreshing file system contents, and re-building the project should make things right again.
来源:https://stackoverflow.com/questions/8204130/strange-nullpointerexception-when-trying-to-retrieve-linearlayout