1

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) 
Gladwin James
  • 493
  • 2
  • 10
  • 21

2 Answers2

2

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 containing another bundle which has no Key-value , in this case this bundle won't have key-value and you are trying to get a boolean from a NULL key object hence NullPointerException

Pavneet_Singh
  • 34,557
  • 5
  • 43
  • 59
  • `getBooleanExtra()` also calls `getExtra()` on the `intent` object – Shaishav Aug 13 '16 at 16:24
  • i know but with `getExtras()` will be like a bundle inside a bundle so which has no bool value in this case hence you gotta use the default bundle with getBooleanExtra – Pavneet_Singh Aug 13 '16 at 16:33
  • `getExtra()` on an `intent` will return the default bundle – Shaishav Aug 13 '16 at 16:36
  • buddy `getExtra()` is deprecated now ,a single 's' can make a difference. – Pavneet_Singh Aug 13 '16 at 16:47
  • buddy, please ignore my typo...like earlier I was still talking about `getExtras()`. – Shaishav Aug 13 '16 at 16:48
  • Dude thankyou for the answer and I'm grateful that you explained the answer thankyou once again @PavneetSingh :) – Gladwin James Aug 13 '16 at 16:53
  • @GladwinJames the answer is correct and won't result in NPE but, I strongly believe his explanation is incorrect. – Shaishav Aug 13 '16 at 16:58
  • My pleasure @Gladwin James , Shishav getExtras() gives a bundle as i said , one with all added values but contained inside new bundle, why would you add overhead when you can use something directly – Pavneet_Singh Aug 13 '16 at 17:00
  • Did you notice, your last comment made your explanation void? – Shaishav Aug 13 '16 at 17:03
  • you wanted a detailed , not useful answer respect to question ,try updated answer – Pavneet_Singh Aug 13 '16 at 17:11
  • Still incorrect because the constructor `public Bundle(bundle b)` makes a copy of the bundle provided (i.e. doesn't tries to get another bundle inside) – Shaishav Aug 13 '16 at 17:13
  • Technically you are wrong and i m too cuz it create a Parcel, so if you want to give a proper depth definition than please suggest an edit and it will be more productive. – Pavneet_Singh Aug 13 '16 at 17:23
  • If I did, I'd have updated my answer. I was striking a conversation with you, seeing your current explanation was incorrect and we might get the correct one! I tried finding on SO for a clearer explanation but, even widely popular answers (that I stumbled upon) couldn't find one :)... Please tag me, SO won't notify me automatically... – Shaishav Aug 13 '16 at 17:29
  • like i was trying to tell with this function you will eventually reach at parcel object which further interact with native code in terms of buffers,streams etc – Pavneet_Singh Aug 13 '16 at 17:39
  • @PavneetSingh If you have time can you please take a look at this [link](http://stackoverflow.com/questions/38932441/android-checkbox-checked-in-one-activity-and-then-button-appears-in-another-acti#comment65233979_38932537) It's not working as it is meant to work. it really means a lot thanks in advance brother. – Gladwin James Aug 14 '16 at 06:10
1

Try the following code (also, not using wrapper class Boolean):

boolean yourBool = getIntent().getBooleanExtra("yourBoolName", false);
Shaishav
  • 5,097
  • 2
  • 18
  • 37