Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange NullpointerException when trying to retrieve LinearLayout
    primarykey
    data
    text
    <p>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).</p> <pre><code> &lt;LinearLayout android:id="@+id/imageListContainer" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"&gt; &lt;/LinearLayout&gt; </code></pre> <p>I then want to populate the LinearLayout dynamically, using this code:</p> <pre><code>LinearLayout imageContainer = (LinearLayout)findViewById(R.id.imageListContainer); </code></pre> <p>from withhin the applications onCreate() callback function.</p> <p>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.</p> <p>I'm pretty much lost as to what the cause of this could be, so I'd appreciate any suggestions or guesses.</p> <p>Here is the stacktrace:</p> <pre><code>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) </code></pre> <p><strong>PS:</strong> I forgot to mention, I also called setContentView(R.layout.main), so that's not the problem either.</p> <p><strong>PSS:</strong> 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload