尝试动态添加按钮到Android活动时的NPE

时间:2014-05-19 22:40:15

标签: java android nullpointerexception

我正在尝试在Button方法中为活动添加onCreate并获得空指针异常;下面是我的代码:

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

第36行是ll.addView(b);

我在其他地方看到这是因为setContentView之前没有调用LinearLayout;但似乎是,或者我错过了什么?

2 个答案:

答案 0 :(得分:0)

由于在setContentView

添加的当前布局中找不到布局ID,因此为空

<强>溶液::

R.layout.activity_main内添加ID为linlayout_main

的线性布局

答案 1 :(得分:0)

我通过在onStart()而不是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);
}

根据onCreate()文档:

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.

根据onStart()文档:

Called when the activity is becoming visible to the user.

基于此,我将动态代码移至onStart(),它运行正常。