Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android ClassCastException on findViewById

I've got a little problem where I can't find the mistake I am making, it's probably something really simple.

I've got the following layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="10dp" >

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:focusable="false" />
    <ImageView
        android:id="@+id/imgStarred"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/checkBox"
        android:layout_below="@+id/checkBox"
        android:layout_marginRight="4dp"
        android:src="@drawable/ic_star_gray"/>


    <CheckedTextView
        android:id="@+id/lblTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/checkBox"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="8dp"
        android:layout_toLeftOf="@+id/imgPriority"
        android:layout_toRightOf="@+id/checkBox"
        android:text="CheckedTextView" />

    <TextView
        android:id="@+id/lblDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/lblTitle"
        android:layout_below="@+id/lblTitle"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/checkBox"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <ImageView
        android:id="@+id/imgPriority"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:src="@drawable/ic_priority_5" />

</RelativeLayout>

When I try to get a view from it with findViewById i get a ClassCast Exception

11-23 10:12:07.680: E/AndroidRuntime(5965): FATAL EXCEPTION: main
11-23 10:12:07.680: E/AndroidRuntime(5965): java.lang.ClassCastException: android.widget.ImageView
11-23 10:12:07.680: E/AndroidRuntime(5965):     at com.bilobait.taskbox.task.TaskBoxTaskList$TaskView.<init>(TaskBoxTaskList.java:459)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at com.bilobait.taskbox.task.TaskBoxTaskList$TaskBoxTaskAdapter.getView(TaskBoxTaskList.java:423)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.widget.AbsListView.obtainView(AbsListView.java:1435)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1256)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.widget.ListView.onMeasure(ListView.java:1167)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.view.View.measure(View.java:8313)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.view.View.measure(View.java:8313)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.view.View.measure(View.java:8313)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.view.View.measure(View.java:8313)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.view.View.measure(View.java:8313)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.view.View.measure(View.java:8313)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.view.View.measure(View.java:8313)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.view.View.measure(View.java:8313)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.view.View.measure(View.java:8313)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.view.View.measure(View.java:8313)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1862)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.os.Looper.loop(Looper.java:123)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at android.app.ActivityThread.main(ActivityThread.java:3695)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at java.lang.reflect.Method.invokeNative(Native Method)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at java.lang.reflect.Method.invoke(Method.java:507)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
11-23 10:12:07.680: E/AndroidRuntime(5965):     at dalvik.system.NativeStart.main(Native Method)

Can somebody help me / give me a hint what i'm missing??

Thx

EDIT: Forgot something, when I take away

android:layout_toLeftOf="@+id/imgPriority"

from CheckedTextView the error is gone.

lblTitle = (TextView)findViewById(R.id.lblTitle);
            lblDescription = (TextView)findViewById(R.id.lblDescription);
            cbCheck = (CheckBox)findViewById(R.id.checkBox);

            imgStar = (ImageView)findViewById(R.id.imgStarred);
            imgPriority = (ImageView)findViewById(R.id.imgPriority);
like image 614
n0n4m3 Avatar asked Nov 23 '11 09:11

n0n4m3


1 Answers

Remove "+" sign from attribute android:layout_toLeftOf="@+id/imgPriority" in CheckedTextView annotation and put ImageView annotation in your layout xml before its id is used

like image 51
a.ch. Avatar answered Sep 27 '22 18:09

a.ch.