Gladwin James Gladwin James - 3 months ago 21
Android Question

Error in Android checkbox checked in one activity and then button appears in another activity

Imagine there are 2 activity's, 'Activity A' and 'Activity B'.'Activity A' holds a checkbox when its checked a button should show on 'Activity B' when its unchecked the button should hide on 'Activity B'. With some support I was able to create few lines but ending up with

java.lang.RuntimeException: Unable to start activity
error

below is the Main activity aka 'Activity A'

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

initializeBubblesManager();


findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addNewBubble();
}
});

CheckBox checkBox = (CheckBox)findViewById(R.id.chkbox1);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

isCheckedValue = isChecked;

Intent intent = new Intent(MainActivity.this, PopUpWindow.class);
intent.putExtra("yourBoolName", isCheckedValue );
startActivity(intent);

}
});
}


Below is the code for popupwindow aka 'Activity B' where i want my button to show and disappear

public class PopUpWindow extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pop_up_window);


DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);

int width = dm.widthPixels;
int height = dm.heightPixels;

getWindow().setLayout((int)(width*.8),(int)(height*.6));

Boolean yourBool = getIntent().getExtras().getBoolean("yourBoolName");
Button fbbutton1 = (Button)findViewById(R.id.fbbutton1);
if(yourBool){
fbbutton1.setVisibility(View.VISIBLE);
}
else{
fbbutton1.setVisibility(View.INVISIBLE);
}


}


Error

08-13 21:08:30.648 31335-31335/com.txusballesteros.bubbles E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.txusballesteros.bubbles, PID: 31335
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.txusballesteros.bubbles/com.txusballesteros.bubbles.PopUpWindow}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.os.Bundle.getBoolean(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.os.Bundle.getBoolean(java.lang.String)' on a null object reference
at com.txusballesteros.bubbles.PopUpWindow.onCreate(PopUpWindow.java:30)
at android.app.Activity.performCreate(Activity.java:6904)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) 
at android.app.ActivityThread.access$1100(ActivityThread.java:229) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:7325) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

Answer

Use this in activity B

getIntent().getBooleanExtra("yourBoolName",false); // false default value .should be provid in case no value received

instead of this

getIntent().getExtras().getBoolean("yourBoolName");

because getExtras() is used to get a Bundle object if it is there so you didn't put any bundle to intent hence code is trying to get a bundle which gives you NULL and you are trying to get a boolean from a NULL object hence NullPointerException