1

我正在开发一个 android 应用程序,我在这里发布后修复了它:Java - Android SDK - Unfrotunately <project name> has crashed error and fix the issues。今天我在搞乱布局,因为我不喜欢它的外观,我试图运行程序并得到一个关于将 a 转换EditText为 a的错误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

4 回答 4

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

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

另请阅读Java 命名约定,其中指出变量应以小写字母开头,以帮助将它们与类区分开来。

于 2013-02-27T19:22:59.847 回答
2

如果您确定 R.id.Check 指的是 a Button,那么您可以尝试清理您的项目,然后再次构建。

于 2013-02-27T19:25:19.303 回答
1
Check = (Button) findViewById(R.id.Check);

似乎您的 R.id.Check 是一个 EditText,而不是一个按钮。

于 2013-02-27T19:22:58.210 回答
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 命名标准,否则您的代码将被评为糟糕的设计,并且会误导许多“快速浏览”的开发人员。

于 2013-02-27T19:32:11.080 回答