0

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

2 回答 2

0
ImageView grid = (ImageView) v;

java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.ImageView

这意味着这v是一个TextView.

因此,您用于网格项目的布局只是一个TextView.

您应该将其更改为Layout包含 anImageView和 a TextView

例如:

网格项目布局

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

OnClickListener

gridView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        ImageView image = (ImageView) v.findViewById(R.id.image);;
        image.setClickable(false);
        image.setImageResource(R.drawable.skin_x);
        TextView text = (TextView) v.findViewById(R.id.text)
        Toast.makeText(getApplicationContext(), text.getText(), Toast.LENGTH_SHORT).show();
    }
});   
于 2013-08-01T10:05:31.623 回答
0

在 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);
    }
});  
于 2013-08-01T09:43:58.400 回答