Note that there are some explanatory texts on larger screens.

plurals
  1. POClassCastException: EditText to Button Error
    primarykey
    data
    text
    <p>I am working on an android app, which I had fixed after posting it here: <a href="https://stackoverflow.com/questions/15098895/java-android-sdk-unfrotunately-project-name-has-crashed-error">Java - Android SDK - Unfrotunately &lt;project name&gt; has crashed error</a> and fixing the issues. Today I was messing about with the layout, because I didn't like the way it looked after which i attempted to run the program and got an error about casting a <code>EditText</code> to a <code>Button</code>, which I'm pretty sure I'm not doing. What causes the <code>ClassCastException</code>? Thank you in advance for your help. </p> <pre><code> 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("") &amp;&amp; !xss.equals("") &amp;&amp; !ys.equals("") &amp;&amp; !yss.equals("")&amp;&amp; xs != null &amp;&amp; xss != null &amp;&amp; ys != null &amp;&amp; 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 &amp;&amp; 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!"); } } }); } } </code></pre> <p>This is the list of errors from the Logcat. </p> <pre><code> 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload