Reputation: 627
i have a question about layouts. When i try to describe buttons with onClickListener i get a Null exception. With buttons everything is ok, but they are on other layout, which is not created when this setup runs. Is this a problem and how to fix it?
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
Upvotes: 0
Views: 49
Reputation: 26198
You cant use/reference a View that is located from another layout thus giving you NPE.
Also you need to have a layout before you reference a Button. setContentView(R.layout.main);
must be declared before you call a Button
reference.
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);
}
});
As you could see that I specified the layout first before referencing a Button
and that layout must contain your Buttons
to avoid NPE.
Upvotes: 2