Java 如何在android中的活动之间传递布尔值?
这就是我目前的做法,但它只是强制关闭应用程序 在第一个活动中Java 如何在android中的活动之间传递布尔值?,java,android,android-intent,Java,Android,Android Intent,这就是我目前的做法,但它只是强制关闭应用程序 在第一个活动中 Intent myIntent = new Intent(Input.this, results.class); myIntent.putExtra("perfect", rig); startActivity(myIntent);` 我想转到的活动 Boolean lovers = getIntent().getExtras().getBoolean("perfect"); 如前所述,函数是getBool
Intent myIntent = new Intent(Input.this, results.class);
myIntent.putExtra("perfect", rig);
startActivity(myIntent);`
我想转到的活动
Boolean lovers = getIntent().getExtras().getBoolean("perfect");
如前所述,函数是getBooleanExtra
Boolean lovers = getIntent().getExtras().getBooleanExtra("perfect");
我不确定被接受的答案::但我认为应该是这样
您可以在发送端尝试以下操作:
MyModel model = new MyModel();
//1. using constructor
Boolean blnObj1 = new Boolean(model.getBooleanStatus()); // This //getBooleanStatus will return 'boolean' value
//2. using valueOf method of Boolean class. This is a static method.
Boolean blnObj2 = Boolean.valueOf(model.getBooleanStatus());
}
Intent targetIntent = new Intent(MyCalass.this, TargetClass.class);
targetIntent.putExtra("STATUS", new Boolean(model.getBooleanStatus()));
targetIntent.putExtra("STATUS", Boolean.valueOf(model.getBooleanStatus()));
startActivity(targetIntent);
接收方:
Intent receiverIntent = getIntent().getBoolean("STATUS");
根据@pearsonatphoto的答案,您可以使用getBooleanExtra方法。但实际上,您自己的代码中没有错误。它应该工作得很好。因此,您在问题中粘贴的代码不会使应用程序崩溃。因此,你最好在应用程序崩溃后粘贴logcat数据。如果你是正确的。getExtras()返回一个Bundle,而Bundle没有getBooleanExtra()方法。
Intent receiverIntent = getIntent().getBoolean("STATUS");