我对布局有疑问。当我尝试使用onClickListener描述按钮时,我得到一个Null异常。使用按钮一切正常,但它们在其他布局上,这在此设置运行时不会创建。这是一个问题以及如何解决它?
mMainViewButton = (Button) findViewById(R.id.btmainview);
mMainViewButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.main);
}
});
mKaireViewButton = (Button) findViewById(R.id.button_kaire);
mKaireViewButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.test_page);
}
});
logcat的:
06-17 08:42:12.120: E/AndroidRuntime(2581): FATAL EXCEPTION: main
06-17 08:42:12.120: E/AndroidRuntime(2581): Process: com.example.android.BluetoothChat, PID: 2581
06-17 08:42:12.120: E/AndroidRuntime(2581): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.BluetoothChat/com.example.android.BluetoothChat.BluetoothChat}: java.lang.NullPointerException
06-17 08:42:12.120: E/AndroidRuntime(2581): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
06-17 08:42:12.120: E/AndroidRuntime(2581): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
06-17 08:42:12.120: E/AndroidRuntime(2581): at android.app.ActivityThread.access$700(ActivityThread.java:135)
06-17 08:42:12.120: E/AndroidRuntime(2581): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
06-17 08:42:12.120: E/AndroidRuntime(2581): at android.os.Handler.dispatchMessage(Handler.java:102)
06-17 08:42:12.120: E/AndroidRuntime(2581): at android.os.Looper.loop(Looper.java:137)
06-17 08:42:12.120: E/AndroidRuntime(2581): at android.app.ActivityThread.main(ActivityThread.java:4998)
06-17 08:42:12.120: E/AndroidRuntime(2581): at java.lang.reflect.Method.invokeNative(Native Method)
06-17 08:42:12.120: E/AndroidRuntime(2581): at java.lang.reflect.Method.invoke(Method.java:515)
06-17 08:42:12.120: E/AndroidRuntime(2581): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:811)
06-17 08:42:12.120: E/AndroidRuntime(2581): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:627)
06-17 08:42:12.120: E/AndroidRuntime(2581): at dalvik.system.NativeStart.main(Native Method)
06-17 08:42:12.120: E/AndroidRuntime(2581): Caused by: java.lang.NullPointerException
06-17 08:42:12.120: E/AndroidRuntime(2581): at com.example.android.BluetoothChat.BluetoothChat.setupChat(BluetoothChat.java:188)
06-17 08:42:12.120: E/AndroidRuntime(2581): at com.example.android.BluetoothChat.BluetoothChat.onStart(BluetoothChat.java:135)
06-17 08:42:12.120: E/AndroidRuntime(2581): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
06-17 08:42:12.120: E/AndroidRuntime(2581): at android.app.Activity.performStart(Activity.java:5253)
06-17 08:42:12.120: E/AndroidRuntime(2581): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2149)
06-17 08:42:12.120: E/AndroidRuntime(2581): ... 11 more
答案 0 :(得分:2)
您无法使用/引用位于其他布局的视图,从而为您提供NPE。
在引用Button之前,还需要有一个布局。在致电setContentView(R.layout.main);
引用之前,必须先声明Button
。
setContentView(R.layout.layout_of_ur_buttons);
mMainViewButton = (Button) findViewById(R.id.btmainview);
mMainViewButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.main);
}
});
mKaireViewButton = (Button) findViewById(R.id.button_kaire);
mKaireViewButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.test_page);
}
});
您可以看到我在引用Button
之前首先指定了布局,并且该布局必须包含您的Buttons
以避免NPE。