gpt4 book ai didi

android - 将数据从一个 Activity 传递到另一个 Activity 会导致强制关闭

转载 作者:行者123 更新时间:2023-12-03 16:28:43 25 4
gpt4 key购买 nike

我是 Android 编程世界的新手,这让我发疯。任何帮助,将不胜感激 !
除了 Intent putextra , getextra 代码部分外,一切正常。因为当我将这部分添加到项目中时,每当我点击一个会开始新 Activity 的按钮时,强制关闭。

下面是 MainActivity.java

public class MainActivity extends Activity {

EditText inputA, inputB, inputC;
TextView nrRoots, root1, root2, roots;
Button bStepByStep, calculate;
Double a, b, c, D, x1, x2, x, dRootNr;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputA = (EditText) findViewById(R.id.etParamA);
inputB = (EditText) findViewById(R.id.etParamB);
inputC = (EditText) findViewById(R.id.etParamC);
nrRoots = (TextView) findViewById(R.id.tvNrRoots);
root1 = (TextView) findViewById(R.id.tvRoot1);
roots = (TextView) findViewById(R.id.tvRoots);
root2 = (TextView) findViewById(R.id.tvRoot2);
calculate = (Button) findViewById(R.id.bCalculate);
bStepByStep = (Button) findViewById(R.id.bStepByStep);

calculate.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
a = Double.parseDouble(inputA.getText().toString());
b = Double.parseDouble(inputB.getText().toString());
c = Double.parseDouble(inputC.getText().toString());

double D = Math.sqrt((b * b) - (4 * a * c));

if (D == 0) {
nrRoots.setText("The equation " + a + "x^2+" + b + "x+" + c
+ "=0 has 1 distinct root.");
x = ((-1) * b) / (2 * a);
root1.setText("x=" + x);
root2.setText("");
roots.setText("");
} else if (D > 0) {
nrRoots.setText("The equation " + a + "x^2+" + b + "x+" + c
+ "=0 has 2 distinct roots.");
x1 = (((-1) * b) + D) / (2 * a);
x2 = (((-1) * b) - D) / (2 * a);
root1.setText("x1= " + x1);
root2.setText("x2= " + x2);
} else {
nrRoots.setText("The equation " + a + "x^2+" + b + "x+" + c
+ "=0 has no distinct roots.");
root1.setText("");
root2.setText("");
}

}

});

bStepByStep.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {

Intent i = new Intent(MainActivity.this, NewActivity.class);
i.putExtra("a", a);
i.putExtra("b", b);
i.putExtra("c", c);
i.putExtra("D", D);
i.putExtra("x1", x1);
i.putExtra("x2", x2);
startActivity(i);
finish();
}
});

}

}

这是NewActivity.java
public class NewActivity extends Activity {

Intent i = getIntent();
Double a = i.getExtras().getDouble("a");
Double b = i.getExtras().getDouble("b");
Double c = i.getExtras().getDouble("c");
Double D = i.getExtras().getDouble("D");
Double x1 = i.getExtras().getDouble("x1");
Double x2 = i.getExtras().getDouble("x2");
TextView bb;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.new_activity);
bb = (TextView) findViewById(R.id.tvArbli);
bb.setText("a=" + a + " " + b + " " + c + " " + D + " " + x1 + " " + x2);
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}

}

我究竟做错了什么 ?

最佳答案

您无法在类实例化期间检索 Intent 。

将您的代码更改为这样的...

public class NewActivity extends Activity {

Intent i;
TextView bb;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.new_activity);

i = getIntent();

Double a = i.getExtras().getDouble("a");
Double b = i.getExtras().getDouble("b");
Double c = i.getExtras().getDouble("c");
Double D = i.getExtras().getDouble("D");
Double x1 = i.getExtras().getDouble("x1");
Double x2 = i.getExtras().getDouble("x2");

bb = (TextView) findViewById(R.id.tvArbli);
bb.setText("a=" + a + " " + b + " " + c + " " + D + " " + x1 + " " + x2);
}

PS你需要做很多验证!

此外,对于任何类型的崩溃,始终包括您的 logcat(堆栈跟踪)。

关于android - 将数据从一个 Activity 传递到另一个 Activity 会导致强制关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21491809/

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