安卓 2.3.3
我正在开发一个自定义计算器。我正在尝试更改 UI,只是在按钮周围随机播放,特别是,当我尝试更改一个按钮时,我得到了 ClassCastException。我尝试“清理”项目,删除 R.java 文件,更改 xml 文件并保存,但没有任何效果。这是来自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
这是没有任何错误的xml文件:::
<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>
当我将电源按钮更改为不同的线性布局时,会发生错误...
发生错误时的 XML
<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>
当我尝试更改电源(id/btnPower)按钮时,我得到了 classcastexception。如果我在 linearlayout9(id/linearlayout9) 中替换它,那么错误就会消失。它编译得很好,但在运行时,它会给出一个异常。
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);
错误不在一个恒定的位置。这取决于我插入电源按钮的位置。我确实阅读了很多关于 ClassCastException 的问题,但我不太明白为什么会导致它。如果有人能详细解释一下,我会很高兴。
如果您需要任何其他信息,请告诉我。
[已解决] 从手机上卸载应用程序,转到项目-> 清理并再次运行。如果错误不断出现,请再次执行这些步骤。谢谢您的帮助。