Android 2.3.3
I am working on a custom calculator. I am trying the change the UI, just shuffling around the buttons and particularly, i get the ClassCastException when i try to change one button.
I tried “Clean”ing the project, deleting the R.java file, changing the xml file and saving, but none worked. Here is the error stack from eclipse :::
01-10 09:19:41.949: E/AndroidRuntime(21494): FATAL EXCEPTION: main
01-10 09:19:41.949: E/AndroidRuntime(21494): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobilevoiceapps.advancedvoicecalculatorplus/com.mobilevoiceapps.advancedvoicecalculatorplus.AdvancedVoiceCalculatorPlus}: java.lang.ClassCastException: android.widget.LinearLayout
01-10 09:19:41.949: E/AndroidRuntime(21494): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
01-10 09:19:41.949: E/AndroidRuntime(21494): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
01-10 09:19:41.949: E/AndroidRuntime(21494): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-10 09:19:41.949: E/AndroidRuntime(21494): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
01-10 09:19:41.949: E/AndroidRuntime(21494): at android.os.Handler.dispatchMessage(Handler.java:99)
01-10 09:19:41.949: E/AndroidRuntime(21494): at android.os.Looper.loop(Looper.java:130)
01-10 09:19:41.949: E/AndroidRuntime(21494): at android.app.ActivityThread.main(ActivityThread.java:3687)
01-10 09:19:41.949: E/AndroidRuntime(21494): at java.lang.reflect.Method.invokeNative(Native Method)
01-10 09:19:41.949: E/AndroidRuntime(21494): at java.lang.reflect.Method.invoke(Method.java:507)
01-10 09:19:41.949: E/AndroidRuntime(21494): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
01-10 09:19:41.949: E/AndroidRuntime(21494): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
01-10 09:19:41.949: E/AndroidRuntime(21494): at dalvik.system.NativeStart.main(Native Method)
01-10 09:19:41.949: E/AndroidRuntime(21494): Caused by: java.lang.ClassCastException: android.widget.LinearLayout
01-10 09:19:41.949: E/AndroidRuntime(21494): at com.mobilevoiceapps.advancedvoicecalculatorplus.AdvancedVoiceCalculatorPlus.onCreate(AdvancedVoiceCalculatorPlus.java:226)
01-10 09:19:41.949: E/AndroidRuntime(21494): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-10 09:19:41.949: E/AndroidRuntime(21494): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
01-10 09:19:41.949: E/AndroidRuntime(21494): ... 11 more
Here is the xml file without any errors:::
<LinearLayout
android:id="@+id/linearLayout9"
android:layout_width="match_parent"
android:layout_height="45px"
android:layout_marginBottom="2px"
android:background="#000000" >
<Button
android:id="@+id/btnPower"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_marginRight="2px"
android:layout_weight="0.20"
android:background="@drawable/button_custom"
android:text="^"
android:textColor="#FFFFFF"
android:textSize="24px" />
<Button
android:id="@+id/btnNthRoot"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_marginRight="2px"
android:layout_weight="0.20"
android:background="@drawable/button_custom"
android:text="n √x"
android:textColor="#FFFFFF"
android:textSize="24px" />
<Button
android:id="@+id/btnLog10"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_marginRight="2px"
android:layout_weight="0.20"
android:background="@drawable/button_custom"
android:text="Log10"
android:textColor="#FFFFFF"
android:textSize="24px" />
<Button
android:id="@+id/btnLN"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_marginRight="2px"
android:layout_weight="0.20"
android:background="@drawable/button_custom"
android:text="Loge"
android:textColor="#FFFFFF"
android:textSize="24px" />
</LinearLayout>
When i change the power button to a different linear layout, the error occurs…
XML when error occurs
<LinearLayout
android:id="@+id/linearLayout4"
android:layout_width="match_parent"
android:layout_height="45px"
android:layout_marginBottom="2px"
android:background="#000000" >
<Button
android:id="@+id/btnInverse"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_marginRight="2px"
android:layout_weight="0.20"
android:background="@drawable/button_custom"
android:text="1/x"
android:textColor="#FFFFFF"
android:textSize="24px" />
<Button
android:id="@+id/btnFactorial"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_marginRight="2px"
android:layout_weight="0.20"
android:background="@drawable/button_custom"
android:text="n!"
android:textColor="#FFFFFF"
android:textSize="24px" />
<Button
android:id="@+id/btnPower"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_marginRight="2px"
android:layout_weight="0.20"
android:background="@drawable/button_custom"
android:text="^"
android:textColor="#FFFFFF"
android:textSize="24px" />
<Button
android:id="@+id/btnPercentage"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_marginRight="2px"
android:layout_weight="0.20"
android:background="@drawable/button_custom"
android:text="%"
android:textColor="#FFFFFF"
android:textSize="24px" />
<Button
android:id="@+id/btnNumberLeftBrace"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_marginRight="2px"
android:layout_weight="0.20"
android:background="@drawable/button_custom"
android:text="("
android:textColor="#FFFFFF"
android:textSize="24px" />
<Button
android:id="@+id/btnNumberRightBrace"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_marginRight="2px"
android:layout_weight="0.20"
android:background="@drawable/button_custom"
android:text=")"
android:textColor="#FFFFFF"
android:textSize="24px" />
</LinearLayout>
When i try to change the power(id/btnPower) button, i get the classcastexception. If i replace it in the linearlayout9(id/linearlayout9) then the error disappears. It compiles well, but at runtime, it will give an exception.
btn0 = (Button) findViewById(R.id.btnNumber0);
btn1 = (Button) findViewById(R.id.btnNumber1); // line with the error
btn2 = (Button) findViewById(R.id.btnNumber2);
btn3 = (Button) findViewById(R.id.btnNumber3);
btn4 = (Button) findViewById(R.id.btnNumber4);
btn5 = (Button) findViewById(R.id.btnNumber5);
btn6 = (Button) findViewById(R.id.btnNumber6);
The error is not at a constant place. It depends on where i insert the power button. I did read quite a few questions on ClassCastException, but i didn’t quite understand why is it being caused. I will be glad if someone can explain it in detail.
Please let me know, if you need any other information.
[SOLVED] Uninstall the application from the mobile, go to project->clean and run again. If the error keeps coming, do follow the steps again. Thanks for the help.
As I already experienced this issue, and lost a lot of time on it, this is just what I did:
A simple clean does not seems to help, the uninstall is more likely to delete strangeness and fix the issue.
Hope this can help.