尝试使用onClick()更改活动时出错

时间:2013-09-25 16:38:52

标签: android android-intent android-activity onclick

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

        }

    }

2 个答案:

答案 0 :(得分:0)

替换

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

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

答案 1 :(得分:0)

您遇到的第一个错误是在按钮上设置监听器时,您应该使用:

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
}

注意:请检查此tutorial,了解如何在活动之间切换