我有一个GridView。在单击每个单独的网格时,我需要在我单击的网格中显示一张图片。我的代码如下:
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
ImageView grid = (ImageView) v;
grid.setClickable(false);
grid.setImageResource(R.drawable.skin_x);
Toast.makeText(getApplicationContext(),
((TextView) v).getText(), Toast.LENGTH_SHORT).show();
}
});
但是当我点击网格时,我的代码崩溃了。上面的代码中是否有任何逻辑错误?有人可以为我的解决方案建议一个好的解决方案。我是android的初学者,所以请不要对我很难。提前致谢! 我的logcat错误报告如下:
08-01 09:43:23.200: E/InputEventReceiver(783): Exception dispatching input event.
08-01 09:43:23.200: E/MessageQueue-JNI(783): Exception in MessageQueue callback: handleReceiveCallback
08-01 09:43:23.240: E/MessageQueue-JNI(783): java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.ImageView
08-01 09:43:23.240: E/MessageQueue-JNI(783): at com.example.ultimatetictactoe.MainActivity$1.onItemClick(MainActivity.java:53)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at android.widget.AbsListView.onTouchEvent(AbsListView.java:3435)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at android.view.View.dispatchTouchEvent(View.java:7246)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at android.view.View.dispatchPointerEvent(View.java:7426)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at android.os.MessageQueue.nativePollOnce(Native Method)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at android.os.MessageQueue.next(MessageQueue.java:125)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at android.os.Looper.loop(Looper.java:124)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at java.lang.reflect.Method.invoke(Method.java:511)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-01 09:43:23.240: E/MessageQueue-JNI(783): at dalvik.system.NativeStart.main(Native Method)
08-01 09:43:23.250: D/AndroidRuntime(783): Shutting down VM
08-01 09:43:23.250: W/dalvikvm(783): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
08-01 09:43:23.290: E/AndroidRuntime(783): FATAL EXCEPTION: main
08-01 09:43:23.290: E/AndroidRuntime(783): java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.ImageView
08-01 09:43:23.290: E/AndroidRuntime(783): at com.example.ultimatetictactoe.MainActivity$1.onItemClick(MainActivity.java:53)
08-01 09:43:23.290: E/AndroidRuntime(783): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
08-01 09:43:23.290: E/AndroidRuntime(783): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
08-01 09:43:23.290: E/AndroidRuntime(783): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
08-01 09:43:23.290: E/AndroidRuntime(783): at android.widget.AbsListView.onTouchEvent(AbsListView.java:3435)
08-01 09:43:23.290: E/AndroidRuntime(783): at android.view.View.dispatchTouchEvent(View.java:7246)
08-01 09:43:23.290: E/AndroidRuntime(783): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168)
08-01 09:43:23.290: E/AndroidRuntime(783): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903)
08-01 09:43:23.290: E/AndroidRuntime(783): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
08-01 09:43:23.290: E/AndroidRuntime(783): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
08-01 09:43:23.290: E/AndroidRuntime(783): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
08-01 09:43:23.290: E/AndroidRuntime(783): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
08-01 09:43:23.290: E/AndroidRuntime(783): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
08-01 09:43:23.290: E/AndroidRuntime(783): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
08-01 09:43:23.290: E/AndroidRuntime(783): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
08-01 09:43:23.290: E/AndroidRuntime(783): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)
08-01 09:43:23.290: E/AndroidRuntime(783): at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
08-01 09:43:23.290: E/AndroidRuntime(783): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901)
08-01 09:43:23.290: E/AndroidRuntime(783): at android.view.View.dispatchPointerEvent(View.java:7426)
08-01 09:43:23.290: E/AndroidRuntime(783): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
08-01 09:43:23.290: E/AndroidRuntime(783): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
08-01 09:43:23.290: E/AndroidRuntime(783): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
08-01 09:43:23.290: E/AndroidRuntime(783): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
08-01 09:43:23.290: E/AndroidRuntime(783): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
08-01 09:43:23.290: E/AndroidRuntime(783): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
08-01 09:43:23.290: E/AndroidRuntime(783): at android.os.MessageQueue.nativePollOnce(Native Method)
08-01 09:43:23.290: E/AndroidRuntime(783): at android.os.MessageQueue.next(MessageQueue.java:125)
08-01 09:43:23.290: E/AndroidRuntime(783): at android.os.Looper.loop(Looper.java:124)
08-01 09:43:23.290: E/AndroidRuntime(783): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-01 09:43:23.290: E/AndroidRuntime(783): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 09:43:23.290: E/AndroidRuntime(783): at java.lang.reflect.Method.invoke(Method.java:511)
08-01 09:43:23.290: E/AndroidRuntime(783): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-01 09:43:23.290: E/AndroidRuntime(783): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-01 09:43:23.290: E/AndroidRuntime(783): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
a查看onItemClick方法中的内容不是ImageView,您应该从该视图中获取imageView。
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
ImageView image = (ImageView) v.findViewById(R.id.grid_image_id);
image.setClickable(false);
image.setImageResource(R.drawable.skin_x);
}
});
答案 1 :(得分:0)
ImageView grid = (ImageView) v;
java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.ImageView
这意味着v
是TextView
。
因此,您对网格项使用的布局只是TextView
。
您应该将其更改为包含Layout
和ImageView
的{{1}}。
例如:
网格项目布局
TextView
<强> OnClickListener 强>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>