我已经声明了变量并收集了有关我的主要活动的数据。我已经设置了意图,并已在我的结果活动中收集了意图。问题似乎是没有将数据从主活动传递到所需的屏幕。
主要目标是在新的活动屏幕上显示结果。
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)
答案 0 :(得分:2)
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);
}
答案 1 :(得分:0)
尝试在您的intent
中获得onCreate()
的结果
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
canvas = new Canvas(this);
setContentView(R.layout.activity_result);
Intent variables = getIntent();
//int length = variables.getIntExtra("varLength",0);
//int width =variables.getIntExtra("varWidth",0);
int totalResult = variables.getIntExtra("varResult",0);
......
......
答案 2 :(得分:0)
在result activity
public class Result extends AppCompatActivity {
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);
final TextView tim = findViewById(R.id.txtResult);
tim.setText(totalResult+"");