15

for some reason, I am getting a ClassCastException error. I don't really know why.

Thanks in advance, I am a new programmer and any help would really help (no pun intended).

Code:

final EditText answerBox = (EditText) findViewById(R.id.answerBox);
    final Button button = (Button) findViewById(R.id.button);
    final TextView problem = (TextView) findViewById(R.id.problem);
    final TextView status = (TextView) findViewById(R.id.status);
    final TextView num = (TextView) findViewById(R.id.numerator);
    final TextView denom = (TextView) findViewById(R.id.denominator);

    button.setOnClickListener(new OnClickListener()
    {
        private String inputString;
        public void onClick(View v)
        {
            inputString = problem.getText().toString();
            int firstNumber = Integer.parseInt(inputString.substring(0,1));
            int secondNumber = Integer.parseInt(inputString.substring(2,3));
            int correct = firstNumber + secondNumber;

            int input;
            if(!answerBox.getText().toString().equals(""))
                input = Integer.parseInt(answerBox.getText().toString());
            else
                input = -1;
            if(input != -1)
            {   
                if(input == correct)
                {
                    status.setText("Nice! You are correct!");
                    denom.setText(Integer.parseInt(denom.getText().toString()) +1);
                    num.setText(Integer.parseInt(num.getText().toString()) +1);

                }
                else
                {
                    status.setText("Sorry, but your answer was wrong.");
                    denom.setText(Integer.parseInt(denom.getText().toString()) +1);
                }
            }

            int a = (int) (Math.random() * 10);
            int b = (int) (Math.random() * 10);

            String newProblem = Integer.toString(a) + "+" + Integer.toString(b);
            problem.setText(newProblem.toString());
            answerBox.setText("");
        }
    });

Once again, thanks.

Stuff from LogCat:

10-30 04:04:10.490: ERROR/AndroidRuntime(551): FATAL EXCEPTION: main
10-30 04:04:10.490: ERROR/AndroidRuntime(551): java.lang.RuntimeException: Unable to start    activity ComponentInfo{com.benhsu.Addition/com.benhsu.Addition.AdditionActivity}: java.lang.ClassCastException: android.widget.TextView
10-30 04:04:10.490: ERROR/AndroidRuntime(551):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-30 04:04:10.490: ERROR/AndroidRuntime(551):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-30 04:04:10.490: ERROR/AndroidRuntime(551):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-30 04:04:10.490: ERROR/AndroidRuntime(551):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-30 04:04:10.490: ERROR/AndroidRuntime(551):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-30 04:04:10.490: ERROR/AndroidRuntime(551):     at android.os.Looper.loop(Looper.java:123)
10-30 04:04:10.490: ERROR/AndroidRuntime(551):     at android.app.ActivityThread.main(ActivityThread.java:3683)
10-30 04:04:10.490: ERROR/AndroidRuntime(551):     at java.lang.reflect.Method.invokeNative(Native Method)
10-30 04:04:10.490: ERROR/AndroidRuntime(551):     at java.lang.reflect.Method.invoke(Method.java:507)
10-30 04:04:10.490: ERROR/AndroidRuntime(551):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-30 04:04:10.490: ERROR/AndroidRuntime(551):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-30 04:04:10.490: ERROR/AndroidRuntime(551):     at dalvik.system.NativeStart.main(Native Method)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): Caused by: java.lang.ClassCastException: android.widget.TextView
10-30 04:04:10.490: ERROR/AndroidRuntime(551):     at com.benhsu.Addition.AdditionActivity.onCreate(AdditionActivity.java:24)
10-30 04:04:10.490: ERROR/AndroidRuntime(551):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-30 04:04:10.490: ERROR/AndroidRuntime(551):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-30 04:04:10.490: ERROR/AndroidRuntime(551):     ... 11 more

XML Layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<TextView  
android:layout_width="fill_parent" 
android:id="@+id/problem" android:layout_height="wrap_content"            android:text="@string/problemString"/>
<EditText android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/answerBox">
<requestFocus></requestFocus>
</EditText>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/status" android:id="@+id/status"></TextView>
<Button android:text="Button" android:layout_height="wrap_content" android:id="@+id/button" android:layout_width="wrap_content"></Button>
<RelativeLayout android:id="@+id/relativeLayout1" android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_weight="0.14">
<TextView android:layout_width="wrap_content" android:layout_alignParentLeft="true" android:layout_height="wrap_content" android:layout_alignBottom="@+id/textView1" android:text="@string/score" android:id="@+id/textView2" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_alignBaseline="@+id/textView1"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/numerator" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/textView2" android:layout_marginLeft="18dp" android:text="@string/numerator"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/fraction" android:id="@+id/fraction" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/numerator" android:layout_marginLeft="14dp"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/denominator" android:id="@+id/denominator" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/fraction" android:layout_marginLeft="19dp"></TextView>
</RelativeLayout>
</LinearLayout>
8
  • Please post the stack trace. We don't have ESP :) Commented Oct 30, 2011 at 4:14
  • I do, but the snowstorm knocked it out. Commented Oct 30, 2011 at 4:16
  • Sorry, I am kinda new, what is a Stack Trace? DO you want me to post the LogCat output stuffs? Commented Oct 30, 2011 at 4:18
  • The list of calls where it tells you you're getting a ClassCastException Commented Oct 30, 2011 at 4:20
  • I highly suspect one of your findViewById() calls is returning null, but we need to know what line of your code is throwing that exception. Commented Oct 30, 2011 at 4:23

1 Answer 1

55

EDIT

It worked fine for me. In Eclipse, go to Project->Clean... and try again. It could be that your R file is out of sync.

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

2 Comments

I posted the XML file in the OP. Thanks for your help
want to +100000000000000000000000000000000000.

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.