Android 2.3.3java.lang.ClassCastException當試圖重新洗牌按鈕
我正在一個自定義計算器。我試圖改變用戶界面,只是在按鈕周圍洗牌,特別是,當我嘗試更改一個按鈕時,出現ClassCastException。 我嘗試「清理」項目,刪除R.java文件,更改xml文件並保存,但沒有任何工作。下面是從蝕:::
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
這裏錯誤堆棧沒有任何錯誤:::
<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)按鈕
XML,我得到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上讀了很多問題,但我不太明白爲什麼會引起這種問題。如果有人能詳細解釋,我會很高興。
請讓我知道,如果你需要任何其他信息。
[已解決]從手機上卸載應用程序,進入項目 - >清理並再次運行。如果錯誤繼續出現,請再次按照步驟操作。謝謝您的幫助。
嘗試去項目 - >清潔 –
正如我已經說過,我清理了項目,但仍然發生異常 –
我已經有這樣奇怪的問題。我從手機中刪除了應用程序並重新編譯/重新安裝。 –