android ClassCastException on findViewById
Asked Answered
A

4

6

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);
Adventuresome answered 23/11, 2011 at 9:16 Comment(5)
What id do you pass to findViewById method?Stamper
Are u typecasting the view properly? It should be like ImageView imgStarred = (ImageView) findViewById(R.id.imgStarred);Bartolommeo
Please provide your java file if possibleSyndicalism
guessing build problem, use Project->Clean.Rescript
Pls. provide java file or provide the line where you get the error.Acridine
S
12

Sorry, I didn't notice by the time of my first answer that you create ids in your references many times. You shouldn't do that :). Remove all "+" signs in all references like layout_toLeftOf, layout_alignRight, layout_below and so on. This will look that way:

<?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 you write, for example, android:layout_toLeftOf="@+id/imgPriority" with plus sign you do create another imgPriority id, and you do not referene already present id of your ImageView.

Stamper answered 23/11, 2011 at 12:33 Comment(3)
thannks a lot...works perfectly now. btw: i used the eclipse layout editor for creating the layout, and it created the wrong ids...maybe a bug?Adventuresome
I also stumbled onto this problem and noticed that cleaning and rebuilding was required after removing the extra plus signs. The extra ids were not removed from the R.id file until rebuilding.Yser
Interesting that this doesn't cause a crash every time or on every device. I just discovered the same issue several months after shipping a version with the stray + signs.Minny
S
3

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

Stamper answered 23/11, 2011 at 9:21 Comment(1)
what @Stamper means is that while using RelativeLayout, you need to use the object's id as a reference only hence android:layout_toLeftOf="@id/imgPriority" The "+" basically means you're assigning an id not referencing it.Flee
L
0

define

<ImageView
        android:id="@+id/imgPriority"
       ........... />

before

CheckedTextView .

in relativeLayout every view related to other should be define after that view

Louvre answered 23/11, 2011 at 9:24 Comment(3)
did it. now im getting the exeptiong when i try to find the lblTitleAdventuresome
dear n0n4m3 . try and understand what does it mean . define one then define and relate two with1 now define three and relate with 1 or 2 . go in this wayLouvre
thats what i did..isn't it possible to relate with 1 and 2 or can i only relate to one...Adventuresome
C
0

Check in your Class file, you must have createed object/Find id of different controls. For example, im my case, when in XML file I had taken image view and I was finding id of image button in Class file, at that time this error had occured.

Creolacreole answered 23/11, 2011 at 9:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.