0

我有以下程序根据用户输入进行计算。但是当我单击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");  

        }

    }
4

2 回答 2

0

代替

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

  Intent myIntent = new Intent(MainActivity.this, CalcResult.class);
于 2013-09-25T16:50:49.663 回答
0

您遇到的第一个错误是在您的 Button 上设置 Listener 时,您应该使用:

calculate = (Button)findViewById(R.id.calc);
            calculate.setOnClickListener(this);//don't cast the listener to OnClickListener

代替 :

calculate = (Button)findViewById(R.id.calc);
            calculate.setOnClickListener((OnClickListener) this);

第二个错误是 Overrided 方法的签名错误,使用:

@Override
public void onClick(View v) {// notice the "o" in lower case
  //your code
}

代替 :

public void OnClick(View v) {
//your code
}

注意:查看本教程以了解如何在活动之间切换

于 2013-09-25T17:11:03.693 回答