我正在开发一个 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