我试图在两个活动之间发送一个Intent值,尽管它看上去很象,已经读过this,但在我的第二个活动中,接收到的intent为null;在运行时加入了NPE。
其背后的预期功能是:“用户扫描活动A中的代码”->“接收到的真实值并打包到意图中”->“活动B打开,解开意图并检查意图值是否为true'->'如果为true,则活动中ImageView的高度减少一定的数量'。
因此,我不确定为什么要在活动B中将我的Intent接收为null,因为我希望进行此检查以便在活动打开时更新高度?
活动A:
//do the handling of the scanned code being true and display
//a dialog message on screen to confirm this
@Override
public void handleResult(Result result) {
final String myResult = result.getText();
Log.d("QRCodeScanner", result.getText());
Log.d("QRCodeScanner", result.getBarcodeFormat().toString());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Activity Complete!");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
//the user has pressed the OK button
@Override
public void onClick(DialogInterface dialog, int which) {
scannerView.resumeCameraPreview(QRActivity.this);
//pack the intent and open our Activity B
Intent intent = new Intent(QRActivity.this, ActivityTank.class);
intent.putExtra("QRMethod", "readComplete");
startActivity(intent);
}
});
builder.setMessage(result.getText());
AlertDialog alert1 = builder.create();
alert1.show();
}
活动B:
// in onCreate, I check that the bundled intent is true
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tank);
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if (extras == null) {
//then the extras bundle is null, and nothing needs to be called here
}
else {
String method = extras.getString("QRmethod");
if (method.equals("readComplete")) {
updateTank();
}
}
}
}
//this is the method that is called when the intent check is true
public int tHeight = 350;
public void updateTank() {
ImageView tankH = (ImageView)findViewById(R.id.tankHeight);
ViewGroup.LayoutParams params = tankH.getLayoutParams();
params.height = tHeight - 35;
tankH.setLayoutParams(params);
}
答案 0 :(得分:2)
在活动B中,您从Intent Extras中拉出QRMethod时遇到错字。您已在使用“ QRmethod
”和“ QRMethod”设置了附加功能。
答案 1 :(得分:1)
您可以使用:
在第一个活动(MainActivity页面)
Intent i = new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("QRmethod","readComplete" );
then you can get it from your second activity by :
在第二个活动中(SecondActivity页面)
Intent intent = getIntent();
String YourtransferredData = intent.getExtras().getString("QRmethod");
答案 2 :(得分:0)
在第二个活动中,您可以使用intent.getStringExtra()
这样的方法来获取字符串值。
if (getIntent() != null){
getIntent().getStringExtra("QRmethod") //this s your "readComplete" value
}