我是一名有用的助手,可以为您翻译相关的IT技术内容。以下是您需要翻译的内容:
我遇到了一个小问题,找不到我所犯的错误,可能是非常简单的问题。我有下面的布局。
当我使用findViewById获取视图时,出现了ClassCast异常。
我遇到了一个小问题,找不到我所犯的错误,可能是非常简单的问题。我有下面的布局。
<?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)
有人能帮我或给我提示,我缺少什么吗?
谢谢
编辑:忘了一些东西,当我去掉
时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);
findViewById
方法传递什么 ID? - a.ch.