我有一个 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)