3

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)
12
  • Please post the full stack trace from logcat. Commented May 1, 2014 at 6:47
  • @Karakuri added stacktrace Commented May 1, 2014 at 6:51
  • 1
    Can you show us VideoPlayerActivity.initShareContextMenu? Commented May 1, 2014 at 6:54
  • @Karakuri no need, it starts from retrieving the ListView. The line I posted above Commented May 1, 2014 at 6:55
  • Well clearly something is wrong, and showing that line of code by itself is not enough information to figure that out. Commented May 1, 2014 at 6:56

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.