gpt4 book ai didi

java - 尝试转换变量并传递给新 Activity 时出错

转载 作者:行者123 更新时间:2023-12-01 04:20:13 26 4
gpt4 key购买 nike

我正在制作一个简单的应用程序来使用用户输入进行计算,但我在使用 Android Math 类进行计算时遇到问题。编译器告诉我

cannot cast from EditText to double

为了使用 atanpow 数学函数,我需要这样做。

我不确定的第二件事是如何在我的 editTexts 中显示带有 id 的计算:mark1 和 mark2。

我的 MainActivity 如下所示:

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

//called when button is clicked.
public void OnClick(View v)
{

//calculations here:
double tri1,tri2;
double marking1,marking2;

marking1 = pow((double)offsetLength,2) + Math.pow((double)offsetDepth,2);
tri1 = (float)offsetDepth/(float)offsetLength;
tri2 = (float)ductDepth/Math.atan((float)tri1);
marking2 = ductDepth/Math.atan(float)(tri2);

//passing the calc results to my CalcResult activity.
Intent myIntent = new Intent(this, CalcResult.class);
myIntent.putExtra("number1", marking1);
myIntent.putExtra("number2", marking2);

startActivity(myIntent);
Intent i = new Intent(this,CalcResult.class);
break;

}

}

这是 CalcResult 类,我“尝试”将两个计算结果传递给它们的 editText:

public class CalcResult extends MainActivity
{
EditText res1,res2;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
res1 = (EditText)findViewById(R.id.mark1);
res2 = (EditText)findViewById(R.id.mark2);

Intent intent = getIntent();
Bundle bundle = intent.getExtras();
Double mark1 = bundle.double("marking1");
Double mark2 = bundle.double("marking2");
}

}

最佳答案

你有这个

 double marking1,marking2; // double
marking1 = pow((double)offsetLength,2) + Math.pow((double)offsetDepth,2);
Intent myIntent = new Intent(this, CalcResult.class);
myIntent.putExtra("number1", marking1);

检索时你有这个

 String mark1 = bundle.getString("marking1"); // retrieving as string

更改为

 double mark1 = bundle.getDouble("marking1"); // similar for mark2

更新

你有这个

 offsetLength = (EditText)findViewById(R.id.offLength);

然后

 marking1 = pow((double)offsetLength,2) + Math.pow((double)offsetDepth,2);

您甚至没有从 editext 中获取值。

点击即可

 String getoffsetlength = offsetLength.getText().toString(); 

现在根据您的要求将字符串转换为 double float

 double off = Double.parseDouble(getoffsetlength);

关于java - 尝试转换变量并传递给新 Activity 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19008995/

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