Taras
Taras

Reputation: 2576

Weird ClassCastException: ImageView cannot be cast to ListView

Weird ClassCastException: ImageView cannot be cast to ListView. In xml the view is declared as ListView. When I retrieve the object as follows:

private void initShareContextMenu() {
    ListView shareListITem = (ListView) findViewById(R.id.share_list);
    shareList = new ContextMenuList(shareListITem,
            (RelativeLayout) findViewById(R.id.content_wrapper), this);

    List<View> views = new ArrayList<View>();
    views.add(findViewById(R.id.share_btn));

    shareList.setViews(views);
}

I got ClassCastException. Tried to build and clean project, remove gen/bin folders, renaming the control but nothing helps. The layout looks as follows:

<RelativeLayout
        android:id="@+id/content_wrapper"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <FrameLayout
            android:id="@+id/youtube_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/black" />

        <ListView
            android:id="@+id/share_list"
            android:layout_width="@dimen/share_context_menu_width"
            android:layout_height="wrap_content"
            android:divider="@color/share_list_divider"
            android:dividerHeight="1px"
            android:visibility="gone" />
    </RelativeLayout>


05-01 09:40:45.215: E/MessageQueue-JNI(18500): Exception in MessageQueue callback: handleReceiveCallback
05-01 09:40:46.096: E/MessageQueue-JNI(18500): java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.ListView
05-01 09:40:46.096: E/MessageQueue-JNI(18500):  at com.packagename.ui.activities.VideoPlayerActivity.initShareContextMenu(VideoPlayerActivity.java:1394)
05-01 09:40:46.096: E/MessageQueue-JNI(18500):  at com.packagename.ui.activities.VideoPlayerActivity.showVideoEndScreen(VideoPlayerActivity.java:1375)
05-01 09:40:46.096: E/MessageQueue-JNI(18500):  at com.packagename.ui.activities.VideoPlayerActivity.onBackPressed(VideoPlayerActivity.java:1587)
05-01 09:40:46.096: E/MessageQueue-JNI(18500):  at android.app.Activity.onKeyUp(Activity.java:2193)
05-01 09:40:46.096: E/MessageQueue-JNI(18500):  at android.view.KeyEvent.dispatch(KeyEvent.java:2664)
05-01 09:40:46.096: E/MessageQueue-JNI(18500):  at android.app.Activity.dispatchKeyEvent(Activity.java:2423)
05-01 09:40:46.096: E/MessageQueue-JNI(18500):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1962)

Upvotes: 3

Views: 376

Answers (1)

Tim Malseed
Tim Malseed

Reputation: 6353

See the answer to a similar question here:

Android: java.lang.ClassCastException: android.widget.imageView cannot be cast to android.widget.textView

Eclipse tends to mess up your resources every now and then. This leads to some odd behavior such as strings and images being swapped all over your app, and more commonly classCastException(s), which happen when Eclipse switches your Views' ids around.

A few solutions to that problem:

Clean your project.

Modify an xml layout file and save.

Delete your R file. (Don't worry it will be automatically generated again).

Basically anything that makes your project rebuild and re-generate the R file.

Upvotes: 1

Related Questions