我有以下程序根据用户输入进行计算。但是当我单击calc
引用的按钮onClick()
时,什么也没有发生。因为它没有导航到结果屏幕。有人可以解释一下吗?不知道为什么 iit 没有改变意图。
我的主要课程是这样的:
public class MainActivity extends Activity implements android.view.View.OnClickListener {
//variables for xml objects
EditText offsetLength,offsetDepth,ductDepth;
Button calculate;
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)
{
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(this, CalcResult.class);
myIntent.putExtra("number1", marking1);
myIntent.putExtra("number2", marking2);
startActivity(myIntent);
Intent i = new Intent(this,CalcResult.class);
} 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");
}
}