Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 尝试使用onClick()更改活动时出错_Android_Android Intent_Android Activity_Onclick - Fatal编程技术网

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

Android 尝试使用onClick()更改活动时出错,android,android-intent,android-activity,onclick,Android,Android Intent,Android Activity,Onclick,我有一个基于用户输入进行计算的程序。但是当我单击onClick()中引用的calc按钮时,什么都没有发生。因为它没有导航到结果屏幕。有人能解释一下吗,因为我不确定iit为什么没有改变意图 我的主要课程是这样的: public class MainActivity extends Activity implements android.view.View.OnClickListener { //variables for xml objects EditText offsetLe

我有一个基于用户输入进行计算的程序。但是当我单击
onClick()
中引用的
calc
按钮时,什么都没有发生。因为它没有导航到结果屏幕。有人能解释一下吗,因为我不确定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");  

        }

    }
替换

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


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

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);
第二个错误是重写方法的签名错误,请使用:

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

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

注意:检查如何在活动之间切换

您的类标题中是否有
实现OnClickListener
?我有
实现android.view.view.OnClickListener
您是否设置了断点以查看它是否进入
onClick()
或不如果您在活动中实现了
OnClickListener
,那么您将需要覆盖
onClick
方法,而不是
onClick
,还需要放置
override
注释以及@ρ;∑ρєK建议,并使用
Log
来记录信息
Log.i(“…”)
而不是
System.out.println
on click listener不是一个注释性的内部类,因此
引用活动上下文,它应该可以正常工作,我认为类似@override public void onClick(视图){}的重写方法那么,这与您所建议的仍然不是op使用的匿名内部类有什么关系呢?那么为什么
这个
在我的CalcResult.class中不起作用呢?我如何让这两个计算出现在我的EditText中,mark1和mark2。我会将result1和result2设置为mark1,mark2吗?这将如何实现无法使用强制转换。@user2815899您需要将double转换为string,如
string.valueOf(doublevalue)
您在LogCat中得到了什么?您在清单文件中声明了第二个活动了吗?@user2815899已在清单中为该活动创建了一个条目,它是否崩溃。如果是这样,请发布堆栈跟踪并删除startActivity之后的额外行
Intent i=new Intent(this,CalcResult.class)
public void OnClick(View v) {
//your code
}