在一个 activity 中选中 Android 复选框时出错,然后按钮出现在另一个 activity 中

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

假设有 2 个 activity,'Activity A' 和 'Activity B'。'Activity A' 持有一个复选框,当它被选中时,一个按钮应该显示在 'Activity B' 上当它未选中时,按钮应该隐藏在 'Activity B' 上。在一些支持下,我能够创建几行,但最终出现 java.lang.RuntimeException: Unable to start activity 错误

下面是主要的 activity 又名 '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);

            }
        });
    }

下面是弹出窗口的代码 'Activity B' 我希望我的按钮在其中显示和消失

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);
            }


        }

错误

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.access00(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.access00(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) 

尝试以下代码(同样,不使用包装器 class Boolean):

boolean yourBool = getIntent().getBooleanExtra("yourBoolName", false);

在activity B

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

而不是这个

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

因为 getExtras() 用于获取包含另一个没有键值的包的 Bundle 对象,在这种情况下,这个包将没有键值,而您正在尝试获取boolean 来自 NULL key object 因此 NullPointerException