findViewById上的android ClassCastException

时间:2011-11-23 09:16:15

标签: android layout classcastexception

我有一个小问题,我找不到我正在犯的错误,这可能是非常简单的事情。

我有以下布局

<?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>

当我尝试使用findViewById从中获取视图时,我得到了ClassCast异常

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)

有人可以帮我/给我一个暗示我错过的东西吗?

THX

编辑:当我带走

时忘记了什么
android:layout_toLeftOf="@+id/imgPriority"
来自CheckedTextView的

错误消失了。

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);

4 个答案:

答案 0 :(得分:12)

抱歉,在我的第一个回答时,我没有注意到您多次在参考文献中创建ID。你不应该这样做:)。删除所有引用中的所有"+"个符号,例如layout_toLeftOflayout_alignRightlayout_below等。这将是这样的:

<?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>

例如,当您编写android:layout_toLeftOf="@+id/imgPriority"加号时,您会创建另一个imgPriority ID,您不参考已经显示您的ImageView的ID

答案 1 :(得分:3)

+注释中的属性android:layout_toLeftOf="@+id/imgPriority"中删除“CheckedTextView”符号,并在使用其ID之前将ImageView注释放入布局xml中

答案 2 :(得分:0)

定义

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

之前

CheckedTextView .
在relativeLayout中的

应该在该视图之后定义与其他相关的每个视图

答案 3 :(得分:0)

签入您的类文件,您必须创建不同控件的对象/查找ID。 例如,在我的情况下,当在XML文件中我已经拍摄了图像视图并且我在类文件中找到了图像按钮的id时,那时发生了这个错误。