【问题标题】:Error in Android checkbox checked in one activity and then button appears in another activity [duplicate]在一个活动中选中Android复选框错误,然后按钮出现在另一个活动中[重复]
【发布时间】:2016-12-20 10:35:03
【问题描述】:

假设有 2 个活动,“活动 A”和“活动 B”。“活动 A”在选中时持有一个复选框,当未选中时,按钮应隐藏在“活动 B”上.在一些支持下,我能够创建几行但以 java.lang.RuntimeException: Unable to start 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);

            }
        });
    }

下面是弹出窗口又名“活动 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.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) 

【问题讨论】:

    标签: java android android-studio checkbox android-checkbox


    【解决方案1】:

    在活动 B 中使用它

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

    而不是这个

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

    因为 getExtras() 用于获取包含另一个没有 Key-value 的包的 Bundle 对象,在这种情况下,该包将没有键值,并且您试图从一个 boolean NULL key object 因此NullPointerException

    【讨论】:

    • getBooleanExtra() 还会在 intent 对象上调用 getExtra()
    • 我知道,但是getExtras() 就像一个包内的包,所以在这种情况下它没有布尔值,因此你必须使用带有 getBooleanExtra 的默认包
    • getExtra() on an intent 将返回默认捆绑包
    • buddy getExtra() 现在已被弃用,一个 's' 就可以发挥作用。
    • 哥们,请忽略我的错字...就像之前我还在谈论getExtras()
    【解决方案2】:

    试试下面的代码(同样,不使用包装类Boolean):

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

    【讨论】:

      猜你喜欢
      • 2016-12-20
      • 1970-01-01
      • 2013-12-06
      • 1970-01-01
      • 1970-01-01
      • 2020-09-30
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      相关资源
      最近更新 更多