gpt4 book ai didi

java - 为什么我的 Intent 无法传递数据并接收intent.getintExtra空对象失败?

转载 作者:行者123 更新时间:2023-12-02 01:27:57 25 4
gpt4 key购买 nike

我已经声明了我的变量并收集了有关我的主要 Activity 的数据。我已经设定了 Intent ,并在我的结果 Activity 中收集了 Intent 。问题似乎是数据没有从主 Activity 传递到所需的屏幕。

主要目标是在新的 Activity 屏幕上显示结果。

public class MainActivity extends AppCompatActivity {

//variables
Button btnCalculate, btnReset;
Spinner spinSpeaker;
EditText editLength, editWidth, editHeight;
String t1,t2,t3;

int numLength, numWidth, numHeight, numResult=0;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//connecting variables to GUI elements
btnCalculate= findViewById(R.id.btnCalculate);
btnReset = findViewById(R.id.btnReset2);
spinSpeaker = findViewById(R.id.spinSpeaker);
editLength= findViewById(R.id.editLength);
editWidth = findViewById(R.id.editWidth);
editHeight = findViewById(R.id.editHeight);




//adding function to the calculate button
btnCalculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//parsing text to integers
t1=editLength.getText().toString();
t2=editWidth.getText().toString();
t3=editHeight.getText().toString();

if (t3.equals("")) {
editHeight.setText("11");
}else if (t2.equals("")) {
editWidth.setText("11");
}else if (t1.equals("")) {
editLength.setText("11");
}else{
numLength = Integer.parseInt(t1);
numWidth = Integer.parseInt(t2);
numHeight = Integer.parseInt(t3);
}

//alert to contact Pro design
if ((numLength>=999)||(numWidth>=999)||(numHeight>=20)) {

Toast.makeText(MainActivity.this, R.string.contactPro,
Toast.LENGTH_LONG).show();
}else {
//calculating volume
numResult = numHeight * numWidth * numLength;

//sending data to other activities
Intent intent = new Intent(MainActivity.this, Result.class);
intent.putExtra("varHeight", numHeight);
intent.putExtra("varWidth", numWidth);
intent.putExtra("varLength", numLength);
intent.putExtra("varResult", numResult);
startActivity(intent);
}
}
});
//the following is the result activity
public class Result extends AppCompatActivity {

//Pulling variables from main activity
Intent variables = getIntent();
//int length = variables.getIntExtra("varLength",0);
//int width =variables.getIntExtra("varWidth",0);
int totalResult = variables.getIntExtra("varResult",0);

String result = String.valueOf(totalResult);
TextView tim;

Canvas canvas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
canvas = new Canvas(this);
setContentView(R.layout.activity_result);
//setContentView(canvas);

tim = findViewById(R.id.txtResult);
tim.setText(result);


我预计 R.id.txtResult 会显示 numResult = numHeight * numWidth * numLength;就在第二个屏幕上。

我不断收到的错误消息是::

引起:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“int android.content.Intent.getIntExtra(java.lang.String, int)” 在 com.example.project.Result.(Result.java:23)

最佳答案

getIntent() 必须在 onCreate() 中调用。将 Result Activity 更改为:

public class Result extends AppCompatActivity {

TextView tim;

Canvas canvas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
canvas = new Canvas(this);
setContentView(R.layout.activity_result);
//setContentView(canvas);

Intent variables = getIntent();
//int length = variables.getIntExtra("varLength",0);
//int width =variables.getIntExtra("varWidth",0);
int totalResult = variables.getIntExtra("varResult",0);

String result = String.valueOf(totalResult);

tim = findViewById(R.id.txtResult);
tim.setText(result);
}

关于java - 为什么我的 Intent 无法传递数据并接收intent.getintExtra空对象失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56570810/

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