错误:无法将Spinner强制转换为EditText

时间:2012-04-06 06:04:35

标签: android android-layout

我不知道这个布局有什么问题:

<ImageView
    android:id="@+id/seticonimage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:adjustViewBounds="true"
    android:contentDescription="@string/description"
    android:scaleType="fitStart"
    android:src="@drawable/cm7_icon" />

<EditText
    android:id="@+id/vsname"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/seticonimage"
    android:hint="@string/entername"
    android:inputType="text" />

<EditText
    android:id="@+id/vsdesc"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/vsname"
    android:layout_toLeftOf="@+id/seticonimage"
    android:hint="@string/shrtdesc"
    android:inputType="text" />

<Spinner
    android:id="@+id/spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/vsdesc"
    android:layout_toLeftOf="@+id/seticonimage"
    android:prompt="@string/icon_prompt" />

<TextView
    android:id="@+id/spinnerinfo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/spinner"
    android:layout_below="@+id/spinner"
    android:layout_toLeftOf="@+id/seticonimage" />

但它以此错误结束:

04-06 06:01:18.203: E/AndroidRuntime(1979): java.lang.ClassCastException: android.widget.Spinner cannot be cast to android.widget.EditText
04-06 06:01:18.203: E/AndroidRuntime(1979):     at com.manager.boot.free.MultiBootManager.onContextItemSelected(MultiBootManager.java:144)
04-06 06:01:18.203: E/AndroidRuntime(1979):     at android.app.Activity.onMenuItemSelected(Activity.java:2509)
04-06 06:01:18.203: E/AndroidRuntime(1979):     at com.android.internal.policy.impl.PhoneWindow$DialogMenuCallback.onMenuItemSelected(PhoneWindow.java:3521)
04-06 06:01:18.203: E/AndroidRuntime(1979):     at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
04-06 06:01:18.203: E/AndroidRuntime(1979):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
04-06 06:01:18.203: E/AndroidRuntime(1979):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
04-06 06:01:18.203: E/AndroidRuntime(1979):     at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:167)
04-06 06:01:18.203: E/AndroidRuntime(1979):     at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:924)
04-06 06:01:18.203: E/AndroidRuntime(1979):     at android.widget.AdapterView.performItemClick(AdapterView.java:292)
04-06 06:01:18.203: E/AndroidRuntime(1979):     at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
04-06 06:01:18.203: E/AndroidRuntime(1979):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
04-06 06:01:18.203: E/AndroidRuntime(1979):     at android.widget.AbsListView$1.run(AbsListView.java:3168)
04-06 06:01:18.203: E/AndroidRuntime(1979):     at android.os.Handler.handleCallback(Handler.java:605)
04-06 06:01:18.203: E/AndroidRuntime(1979):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-06 06:01:18.203: E/AndroidRuntime(1979):     at android.os.Looper.loop(Looper.java:137)
04-06 06:01:18.203: E/AndroidRuntime(1979):     at android.app.ActivityThread.main(ActivityThread.java:4424)
04-06 06:01:18.203: E/AndroidRuntime(1979):     at java.lang.reflect.Method.invokeNative(Native Method)
04-06 06:01:18.203: E/AndroidRuntime(1979):     at java.lang.reflect.Method.invoke(Method.java:511)
04-06 06:01:18.203: E/AndroidRuntime(1979):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-06 06:01:18.203: E/AndroidRuntime(1979):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-06 06:01:18.203: E/AndroidRuntime(1979):     at dalvik.system.NativeStart.main(Native Method)

我在这里做错了什么?

        final View textEntryView = factory.inflate(R.layout.editvslayout,
                null);
        final EditText evsname = (EditText) textEntryView
                .findViewById(R.id.vsname);
        final EditText evsdesc = (EditText) textEntryView
                .findViewById(R.id.vsdesc);
        final Spinner spinner = (Spinner) textEntryView
                .findViewById(R.id.spinner);
        final ImageView i = (ImageView) textEntryView
                .findViewById(R.id.seticonimage);

4 个答案:

答案 0 :(得分:5)

请清理您的项目,因为您可能已为SpinnerR.java版本指定了ID,但稍后您将EditText的相同名称指定给R.java而不构建{{1} }}

由于R.java保留了所有控件的引用,所以在第一次分配给微调器时R.java认为这个id是一个微调器,但现在你将它分配给EditText并且R.java完全没有意识到你清洁和建设。

道德:有时你必须手动建立R.java

答案 1 :(得分:4)

看起来你的代码如下:

Spinner mSpinner = (EditText) findViewById (R.id.spinner);

相反,它应该正确地转换为:

Spinner mSpinner = (Spinner) findViewById (R.id.spinner);

答案 2 :(得分:0)

如果您正在使用eclipse,请转到项目 - &gt; 清洁希望能够解决问题。它确实为我解决了。

答案 3 :(得分:0)

使用Android Studio / IntelliJ 15我经常需要构建/清理项目&#39;几次。当它不起作用(这是经常)时,我最终删除了Build目录。然后它工作。我无法提供理由,但我非常喜欢IntelliJ。就我而言,IntelliJ正在混合视图ID的值。