viewstub的ClasscastException

时间:2011-06-29 11:57:54

标签: android

我正在尝试将ViewStub类型转换为另一个类类型,但在运行.apk文件时,当我点击新屏幕时,模拟器会抛出异常。

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>

1 个答案:

答案 0 :(得分:0)

抛出

ClassCastException是因为您试图将FrameLayout投射到RecipientsEditor。改变这一行:

  

mRecipientsEditor =(RecipientsEditor)stub.inflate();

mRecipientsEditor = stub.inflate();,并将mRecipientsEditor的声明类型更改为View