1

I am working on an android app, which I had fixed after posting it here: Java - Android SDK - Unfrotunately <project name> has crashed error 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 EditText to a Button, which I'm pretty sure I'm not doing. What causes the ClassCastException? Thank you in advance for your help.

  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!");
            }
        }
    });
}       
}

This is the list of errors from the 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
1
  • 1
    -1 this means LITERALLY what exception said Commented Feb 27, 2013 at 19:23

4 Answers 4

2

If you are sure that R.id.Check refers to a Button, then you can try cleaning your project, and building again.

Sign up to request clarification or add additional context in comments.

Comments

2

According to the error:

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 refers to an EditText not a Button, here.

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

Change either the Java code or the XML in main.xml depending on which View type you actually want.

Also please read about Java naming convention which states that variables should start with a lowercase letter to help distinguish them from classes.

1 Comment

yeah sry im better with C++ and C# and havent dont much with Java and classes as a whole. Thank you for your help :)
1
Check = (Button) findViewById(R.id.Check);

seems like your R.id.Check is an EditText, not a Button.

Comments

1

Error is here

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

It is because of this

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

That error says you are trying to convert an EditText which is not a button.

Go to your XML file where the related layout is designed and change the android ID value for the particular fields

Edit

It seems like you are coming from C++ background. In Java, please follow Java naming standards, otherwise your code will be rated as a bad design and will misguide lot of "quick look" developers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.