【发布时间】:2018-08-07 15:55:04
【问题描述】:
我正在尝试在两个活动之间发送一个 Intent 值,尽管在阅读this 之后,在我的第二个活动中,收到的意图为空;在运行时遇到了 NPE。
这背后的预期功能是:'用户在 Activity A 中扫描代码' -> '收到并打包到 Intent 中的真实值' -> 'Activity B 打开,解包 Intent 并检查 Intent 值是否为true' -> '如果为 true,则 Activity 中 ImageView 的高度会减少一定的量'。
因此,我不确定为什么我的 Intent 在 Activity B 中被接收为 null,因为我希望进行此检查以便在 Activity 打开时更新高度?
活动 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);
}
【问题讨论】:
标签: java android android-intent android-activity