gpt4 book ai didi

java - 两个 Activity 之间的 Intent 出现 NullPointerException

转载 作者:行者123 更新时间:2023-11-29 04:16:58 25 4
gpt4 key购买 nike

我正在尝试在两个 Activity 之间发送一个 Intent 值,尽管看起来已经阅读了 this ,在我的第二个 Activity 中,收到的 Intent 是空的;在运行时遇到 NPE。

这背后的预期功能是:“用户在 Activity A 中扫描代码”->“收到真实值并将其打包到 intent 中”->“Activity B 打开、解压缩 intent 并检查 intent 值是true' -> '如果为 true, Activity 中 ImageView 的高度将减少一个设定的量'。

因此,我不确定为什么我的 Intent 在 Activity B 中被接收为 null,因为我希望进行此检查以便在 Activity 打开时更新高度?

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();
}

Activity 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);
}

最佳答案

在 Activity B 中,您在从 Intent extras 中提取 QRMethod 时出现错字。您正在使用 QRmethod,而您已经使用“QRMethod”设置了附加功能。

关于java - 两个 Activity 之间的 Intent 出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51730957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com