ClassCastException:EditText to Button Error

时间:2013-02-27 19:20:42

标签: java android runtime-error classcastexception

我正在开发一个Android应用程序,我在此处发布后修复了该问题:Java - Android SDK - Unfrotunately <project name> has crashed error并解决问题。今天我一直在搞乱布局,因为我不喜欢它试图运行程序的方式,并且在将EditText转换为Button时遇到错误,我是我很确定我没做。导致ClassCastException的原因是什么?预先感谢您的帮助。

  package complex.OliverV;

  import android.app.Activity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;
  import android.widget.RadioButton;
  import android.widget.EditText;
  public class ComplexNumbersActivity extends Activity {
/** Called when the activity is first created. */
Button Check;
RadioButton plus, minus, multiply, div;
EditText X1,X2,Y1,Y2;
TextView Ans;
int sign;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Check = (Button) findViewById(R.id.Check);
    plus = (RadioButton) findViewById(R.id.plus);
    minus = (RadioButton) findViewById(R.id.minus);
    multiply = (RadioButton) findViewById(R.id.multiply);
    div = (RadioButton) findViewById(R.id.div);
    Ans = (TextView) findViewById(R.id.Ans);
    X1=(EditText) findViewById(R.id.X1);
    X2=(EditText) findViewById(R.id.X2);
    Y1=(EditText) findViewById(R.id.Y1);
    Y2=(EditText) findViewById(R.id.Y2);
    plus.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            sign=1;
        }


    });
    minus.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            sign=2;
        }


    });
    multiply.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            sign=3;
        }


    });
    div.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            sign=4;
        }


    });
    Check.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String xs=X1.getText().toString();
            String xss=X2.getText().toString();
            String ys=Y1.getText().toString();
            String yss=Y2.getText().toString();
            double x3 = 0, y3 = 0;
            if(!xs.equals("") && !xss.equals("") && !ys.equals("") && !yss.equals("")&& xs != null && xss != null && ys != null && yss != null)
            {
            double x1=Double.parseDouble(xs);
            double x2=Double.parseDouble(xss);
            double y1=Double.parseDouble(ys);
            double y2=Double.parseDouble(yss);
            switch(sign)
            {
            case(1):
            {
                x3=x1+x2;
                y3=y1+y2;
            }
            case(2):
            {
                x3=x1-x2;
                y3=y1-y2;
            }
            case(3):
            {
                x3=(x1*x2 - y1*y2);
                y3=(x2*y1 + x1*y2);
            }
            case(4):
            {
                if(x2!=0 && y2!=0)
                {
                x3 = (x1 * x2 + y1 * y2) / (x2 * x2 + y2 * y2);
                y3 = (x2 * y1 - x1 * y2) / (x2 * x2 + y2 * y2);
                }
                else
                {
                    Ans.setText("Enter valid numbers!");
                }
            }
        }
            Ans.setText("x = "+x3+"y = "+y3);
        }
            else
            {
                Ans.setText("Enter valid numbers!");
            }
        }
    });
}       
}

这是Logcat的错误列表。

  02-27 21:04:48.679: E/AndroidRuntime(571): FATAL EXCEPTION: main
  02-27 21:04:48.679: E/AndroidRuntime(571): java.lang.RuntimeException: Unable to start activity ComponentInfo{complex.OliverV/complex.OliverV.ComplexNumbersActivity}: java.lang.ClassCastException: android.widget.EditText cannot be cast to android.widget.Button
  02-27 21:04:48.679: E/AndroidRuntime(571):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
  02-27 21:04:48.679: E/AndroidRuntime(571):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
  02-27 21:04:48.679: E/AndroidRuntime(571):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
  02-27 21:04:48.679: E/AndroidRuntime(571):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
  02-27 21:04:48.679: E/AndroidRuntime(571):    at android.os.Handler.dispatchMessage(Handler.java:99)
  02-27 21:04:48.679: E/AndroidRuntime(571):    at android.os.Looper.loop(Looper.java:137)
  02-27 21:04:48.679: E/AndroidRuntime(571):    at android.app.ActivityThread.main(ActivityThread.java:4424)
  02-27 21:04:48.679: E/AndroidRuntime(571):    at java.lang.reflect.Method.invokeNative(Native Method)
  02-27 21:04:48.679: E/AndroidRuntime(571):    at java.lang.reflect.Method.invoke(Method.java:511)
  02-27 21:04:48.679: E/AndroidRuntime(571):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
  02-27 21:04:48.679: E/AndroidRuntime(571):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
  02-27 21:04:48.679: E/AndroidRuntime(571):    at dalvik.system.NativeStart.main(Native Method)
  02-27 21:04:48.679: E/AndroidRuntime(571): Caused by: java.lang.ClassCastException: android.widget.EditText cannot be cast to android.widget.Button
  02-27 21:04:48.679: E/AndroidRuntime(571):    at complex.OliverV.ComplexNumbersActivity.onCreate(ComplexNumbersActivity.java:21)
  02-27 21:04:48.679: E/AndroidRuntime(571):    at android.app.Activity.performCreate(Activity.java:4465)
  02-27 21:04:48.679: E/AndroidRuntime(571):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
  02-27 21:04:48.679: E/AndroidRuntime(571):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
  02-27 21:04:48.679: E/AndroidRuntime(571):    ... 11 more

4 个答案:

答案 0 :(得分:2)

根据错误:

Caused by: java.lang.ClassCastException: android.widget.EditText cannot be cast to android.widget.Button
    at complex.OliverV.ComplexNumbersActivity.onCreate(ComplexNumbersActivity.java:21) 

R.id.Check在这里指的是EditText而非Button。

    Check = (Button) findViewById(R.id.Check);  // Line 21

根据您实际需要的View类型,更改main.xml中的Java代码或XML。

另请阅读Java naming convention,其中指出变量应以小写字母开头,以帮助区分它们。

答案 1 :(得分:2)

如果您确定R.id.Check引用Button,那么您可以尝试清理项目,然后重新构建。

答案 2 :(得分:1)

Check = (Button) findViewById(R.id.Check);

看起来你的R.id.Check是一个EditText,而不是一个Button。

答案 3 :(得分:1)

错误就在这里

Check = (Button) findViewById(R.id.Check);

正是因为这个

E/AndroidRuntime(571): Caused by: java.lang.ClassCastException: android.widget.EditText cannot be cast to android.widget.Button

该错误表示您正在尝试转换不是按钮的EditText。

转到设计相关布局的XML文件,并更改特定字段的android ID值

修改

好像你来自C ++背景。在Java中,请遵循Java命名标准,否则您的代码将被评为糟糕的设计,并会误导许多“快速查看”的开发人员。