0

I'm trying to add a Button in the onCreate method for an activity and am getting a Null Pointer exception; below is my code:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // dynamic content
        Button b = new Button(this);
        b.setText("test");

        LinearLayout ll = (LinearLayout) findViewById(R.id.linlayout_main);
        ll.addView(b);
    }

Logcat:

05-19 18:42:54.658: D/AndroidRuntime(2338): Shutting down VM
05-19 18:42:54.658: W/dalvikvm(2338): threadid=1: thread exiting with uncaught exception (group=0xb4a67ba8)
05-19 18:42:54.668: E/AndroidRuntime(2338): FATAL EXCEPTION: main
05-19 18:42:54.668: E/AndroidRuntime(2338): Process: net.test.Rem, PID: 2338
05-19 18:42:54.668: E/AndroidRuntime(2338): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.test.Rem/net.test.Rem.MainActivity}: java.lang.NullPointerException
05-19 18:42:54.668: E/AndroidRuntime(2338):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-19 18:42:54.668: E/AndroidRuntime(2338):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-19 18:42:54.668: E/AndroidRuntime(2338):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-19 18:42:54.668: E/AndroidRuntime(2338):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-19 18:42:54.668: E/AndroidRuntime(2338):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-19 18:42:54.668: E/AndroidRuntime(2338):     at android.os.Looper.loop(Looper.java:136)
05-19 18:42:54.668: E/AndroidRuntime(2338):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-19 18:42:54.668: E/AndroidRuntime(2338):     at java.lang.reflect.Method.invokeNative(Native Method)
05-19 18:42:54.668: E/AndroidRuntime(2338):     at java.lang.reflect.Method.invoke(Method.java:515)
05-19 18:42:54.668: E/AndroidRuntime(2338):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-19 18:42:54.668: E/AndroidRuntime(2338):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-19 18:42:54.668: E/AndroidRuntime(2338):     at dalvik.system.NativeStart.main(Native Method)
05-19 18:42:54.668: E/AndroidRuntime(2338): Caused by: java.lang.NullPointerException
05-19 18:42:54.668: E/AndroidRuntime(2338):     at net.test.Rem.MainActivity.onCreate(MainActivity.java:36)
05-19 18:42:54.668: E/AndroidRuntime(2338):     at android.app.Activity.performCreate(Activity.java:5231)
05-19 18:42:54.668: E/AndroidRuntime(2338):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-19 18:42:54.668: E/AndroidRuntime(2338):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-19 18:42:54.668: E/AndroidRuntime(2338):     ... 11 more

Line 36 is ll.addView(b);

I read elsewhere on SO that this is because the setContentView is not being called before LinearLayout; but it seems to be, or am I missing something?

  • Is `linlayout_main` in `activity_main`? If so, post your logcat output. The way it looks is correct. – codeMagic May 19 '14 at 22:42
  • Nope. Right now it is looking in the wrong layout – codeMagic May 19 '14 at 22:50
  • @TonyHopkinson Not a duplicate of that question. – Lily Chung May 19 '14 at 23:15
  • @josten. I don't care it's an NPE, you got to the line of code, you work backwards and you discover why it's null, then you test for null or make sure it's never null. Only other option is to keep a question and answer for every possible scenario, which among other things, would be self defeating. – Tony Hopkinson May 20 '14 at 01:06
  • See http://stackoverflow.com/questions/23653778/nullpointerexception-accessing-views-in-oncreate - the last paragraph also mentions why it works for you in `onStart()` – laalto May 20 '14 at 05:01
  • @Josten we seem to be at cross purposes. I interpreted your question as I have an NPE please help. You seem to have meant how do I make this badly designed component work. Might be worth bearing in mind we get a lot of NPE questions, so de-emphasising that aspect of the problem would be wise. – Tony Hopkinson May 20 '14 at 11:03

2 Answers2

0

It is null due to layout id was not found in your current layout added i your setContentView

solution::

add a linear layout inside your R.layout.activity_main with the id of linlayout_main

Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
0

I resolved this issue by dynamically adding content within onStart() instead of onCreate():

protected void onStart() {
    super.onStart();
    // dynamic content
    Button b = new Button(this);
    b.setText("test");

    LinearLayout ll = (LinearLayout) findViewById(R.id.linlayout_main);
    System.out.println(ll);
    ll.addView(b);
}

According to onCreate() docs:

Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc.

According to onStart() docs:

Called when the activity is becoming visible to the user.

Based on this I moved my dynamic code to onStart() and it works just fine.