我正在尝试将两个数字输入从一个活动传输到另一个活动的 UI,但是当我单击按钮更改意图时它崩溃了。
我在 logcat 中得到以下信息:http: //pastebin.com/zkWPcSNZ,这表明我将数据解析到 CalcResult 中的 editTexts 的方式存在问题。
我的问题是我尝试将数据传递给 CalcResult editText 的方式有什么问题。是否有其他方法可以实现这一点?
我的两个类看起来像这样供参考:
public class MainActivity extends Activity implements OnClickListener {
//variables for xml objects
EditText offsetLength,offsetDepth,ductDepth;
Button calculate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//setting the variables to the xml id's and setting the click listener on the calc button
offsetLength = (EditText)findViewById(R.id.offLength);
offsetDepth = (EditText)findViewById(R.id.offDepth);
ductDepth = (EditText)findViewById(R.id.ductDepth);
calculate = (Button)findViewById(R.id.calc);
calculate.setOnClickListener(this);//don't cast the listener to OnClickListener
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
String getoffsetlength = offsetLength.getText().toString();
String getoffsetdepth = offsetDepth.getText().toString();
String getductdepth = ductDepth.getText().toString();
double tri1,tri2;
double marking1,marking2;
double off1 = Double.parseDouble(getoffsetlength);
double off2 = Double.parseDouble(getoffsetdepth);
double off3 = Double.parseDouble(getductdepth)
;
marking1 = Math.pow(off1,2) + Math.pow(off2,2);
tri1 = (float)off2/(float)off1;
tri2 = (float)off3/Math.atan((float)tri1);
marking2 = (float)off3/Math.atan(tri2);
Intent myIntent = new Intent(MainActivity.this, CalcResult.class);
myIntent.putExtra("numbers", marking1);
myIntent.putExtra("numbers", marking2);
startActivity(myIntent);
} catch (NumberFormatException e) {
// TODO: handle exception
System.out.println("Must enter a numeric value!");
}
}
}
这是我将数据传递给的活动:
public class CalcResult extends MainActivity
{
EditText result1,result2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
result1 = (EditText)findViewById(R.id.mark1);
result2 = (EditText)findViewById(R.id.mark2);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
double mark1 = bundle.getDouble("number1");
double mark2 = bundle.getDouble("number2");
int a = Integer.valueOf(result1.getText().toString());
int b = Integer.valueOf(result2.getText().toString());
result1.setText(a + " ");
result2.setText(b + " ");
}
}