我正在尝试在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
;但似乎是,或者我错过了什么?
答案 0 :(得分:0)
由于在setContentView
<强>溶液:: 强>
在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()
,它运行正常。