Java 在一个活动中选中Android错误复选框,然后在另一个活动中出现按钮
假设有两个活动,“活动A”和“活动B”。“活动A”在选中时保留一个复选框A按钮应显示在“活动B”上,在未选中时按钮应隐藏在“活动B”上。有了一些支持,我可以创建几行代码,但最后出现了Java 在一个活动中选中Android错误复选框,然后在另一个活动中出现按钮,java,android,android-studio,checkbox,android-checkbox,Java,Android,Android Studio,Checkbox,Android Checkbox,假设有两个活动,“活动A”和“活动B”。“活动A”在选中时保留一个复选框A按钮应显示在“活动B”上,在未选中时按钮应隐藏在“活动B”上。有了一些支持,我可以创建几行代码,但最后出现了java.lang.RuntimeException:无法启动活动错误 以下是主要活动,也称为“活动A” @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceSta
java.lang.RuntimeException:无法启动活动
错误
以下是主要活动,也称为“活动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);
}
});
}
下面是popupwindow又名“活动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)
请尝试以下代码(也不要使用包装类Boolean
):
在活动B中使用此选项
getIntent().getBooleanExtra("yourBoolName",false); // false default value .should be provid in case no value received
而不是这个
getIntent().getExtras().getBoolean("yourBoolName");
由于
getExtras()
用于获取包含另一个没有键值的Bundle的Bundle
对象,因此在这种情况下,此Bundle将没有键值,并且您试图从NULL Key对象获取布尔值
还调用intent
对象上的getExtra()
,我知道,但是使用getExtras()
将类似于捆绑包中的一个捆绑包,因此在这种情况下没有bool值,因此您必须使用默认捆绑包与getBooleanExtragetExtra()
在上,intent
将返回默认的bundle buddygetExtra()
现在已被弃用,单个的“s”可能会有所不同。伙计,请忽略我的打字错误…就像我之前还在谈论的getExtras()
一样。
getIntent().getExtras().getBoolean("yourBoolName");