2011-06-29 22 views
0

我想將一個ViewStub類型轉換爲另一個類類型,但運行.apk文件時,當我點擊新屏幕時,模擬器拋出異常。ClasscastException for viewstub

的DDMS logcat中有下列例外

**06-29 17:21:40.354: ERROR/AndroidRuntime(297): **java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.locive/com.android.locive.ui.ComposeMessageActivity}: java.lang.ClassCastException: android.widget.FrameLayout**** 
06-29 17:21:40.354: ERROR/AndroidRuntime(297):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
06-29 17:21:40.354: ERROR/AndroidRuntime(297):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
06-29 17:21:40.354: ERROR/AndroidRuntime(297):  at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
06-29 17:21:40.354: ERROR/AndroidRuntime(297):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
06-29 17:21:40.354: ERROR/AndroidRuntime(297):  at android.os.Handler.dispatchMessage(Handler.java:99) 
06-29 17:21:40.354: ERROR/AndroidRuntime(297):  at android.os.Looper.loop(Looper.java:123) 
06-29 17:21:40.354: ERROR/AndroidRuntime(297):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
06-29 17:21:40.354: ERROR/AndroidRuntime(297):  at java.lang.reflect.Method.invokeNative(Native Method) 
06-29 17:21:40.354: ERROR/AndroidRuntime(297):  at java.lang.reflect.Method.invoke(Method.java:521) 
06-29 17:21:40.354: ERROR/AndroidRuntime(297):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
06-29 17:21:40.354: ERROR/AndroidRuntime(297):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
06-29 17:21:40.354: ERROR/AndroidRuntime(297):  at dalvik.system.NativeStart.main(Native Method) 
**06-29 17:21:40.354: ERROR/AndroidRuntime(297): Caused by: java.lang.ClassCastException: android.widget.FrameLayout 
06-29 17:21:40.354: ERROR/AndroidRuntime(297):  at com.android.locive.ui.ComposeMessageActivity.initRecipientsEditor(ComposeMessageActivity.java:1607) 
06-29 17:21:40.354: ERROR/AndroidRuntime(297):  at com.android.locive.ui.ComposeMessageActivity.initialize(ComposeMessageActivity.java:1777) 
06-29 17:21:40.354: ERROR/AndroidRuntime(297):  at com.android.locive.ui.ComposeMessageActivity.onCreate(ComposeMessageActivity.java:1690) 
06-29 17:21:40.354: ERROR/AndroidRuntime(297):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
06-29 17:21:40.354: ERROR/AndroidRuntime(297):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
06-29 17:21:40.354: ERROR/AndroidRuntime(297):  ... 11 more 
06-29 17:21:40.443: WARN/ActivityManager(59): Force finishing activity com.android.locive/.ui.ComposeMessageActivity 
06-29 17:21:40.453: WARN/ActivityManager(59): Force finishing activity com.android.locive/.ui.ConversationList 
06-29 17:21:40.953: WARN/ActivityManager(59): Activity pause timeout for HistoryRecord{450f7ad8 com.android.locive/.ui.ComposeMessageActivity} 
06-29 17:21:41.014: INFO/ARMAssembler(59): generated scanline__00000077:03515104_00000000_00000000 [ 33 ipp] (47 ins) at [0x384800:0x3848bc] in 452917 ns 
06-29 17:21:51.992: WARN/ActivityManager(59): Activity destroy timeout for HistoryRecord{44fbc270 com.android.locive/.ui.ConversationList} 
06-29 17:21:51.994: WARN/ActivityManager(59): Activity destroy timeout for HistoryRecord{450f7ad8 com.android.locive/.ui.ComposeMessageActivity}** 

的方法是

private void initRecipientsEditor() { 
    if (isRecipientsEditorVisible()) { 
     return; 
    } 
    // Must grab the recipients before the view is made visible because getRecipients() 
    // returns empty recipients when the editor is visible. 
    ContactList recipients = getRecipients(); 

    ViewStub stub = (ViewStub) findViewById(R.id.recipients_editor_stub); 
    if (stub != null) { 
     //stub.inflate(); 
     **mRecipientsEditor = (RecipientsEditor) stub.inflate();** 
    } else { 
     mRecipientsEditor = (RecipientsEditor)findViewById(R.id.recipients_editor); 
     mRecipientsEditor.setVisibility(View.VISIBLE); 
    } 

爲RecipientsEditor類XML文件是

FrameLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

<!-- Layout for edittext and button --> 



    <EditText 
     android:id="@+id/recipients_editor" 
     android:hint="@string/to_hint" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:lines="1" 
        /> 

<!-- 
    <style name="PlusButton"> 
<item name="android:background">@drawable/btn_circle</item> 
<item name="android:src">@drawable/ic_btn_round_plus</item> 
<item name="android:contentDescription">@string/description_plus_button</item> 
</style> 
--> 

    <ImageButton 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:layout_margin="3dip" 
     android:src="@drawable/minibtn" 
     /> 


</FrameLayout> 

回答

0

ClassCastException被拋出,因爲你正試圖將FrameLayout轉換爲RecipientsEditor。更改此行:

mRecipientsEditor =(RecipientsEditor)stub.inflate();

mRecipientsEditor = stub.inflate();,改變聲明的類型的mRecipientsEditorView