修复 Gridlayout 投射到 ImageView 的错误
Fixing error with Gridlayout casting to ImageView
我将 .xml
文件设置为
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:columnCount="4"
android:rowCount="6"
android:onClick="dropIn"
android:layout_alignParentEnd="true"
android:id="@+id/gridLayout">
<ImageView
android:id="@+id/imageView1"
android:layout_width="80dp"
android:layout_height="70dp"
android:layout_column="0"
tools:layout_editor_absoluteX="288dp"
tools:layout_editor_absoluteY="180dp"
android:layout_row="0"
android:onClick="dropIn"
android:contentDescription="@null"
android:tag="1"
app:srcCompat="@drawable/stone" />
无论如何,我有更多的 ImageView,而使它们与众不同的是 ID 和标签。我决定让代码通过标签识别它们。所以这就是我目前拥有的:
int[] gameState = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
public void dropIn(View view) {
ImageView counter = (ImageView) view;
counter.setImageResource(R.drawable.stone);
int tappedCounter = Integer.parseInt(counter.getTag().toString());
try {
if (gameState[tappedCounter] == 2 && gameIsActive) {
gameState[tappedCounter] = 3;
countMe = countMe - 1;
counter.setVisibility(View.INVISIBLE);
} else if (gameState[tappedCounter] == 3 && gameIsActive) {
Toast.makeText(MainActivity.this, "That field is already played!",
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(MainActivity.this, "Oho!",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
这一切工作正常,除了当我点击一个已经被点击但现在不可见的 ImageView
时,它使应用程序崩溃并显示以下消息:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.nedim.lastonestanding, PID: 9758
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:5374)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:5369)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.ClassCastException: android.widget.GridLayout cannot be cast to android.widget.ImageView
at com.nedim.lastonestanding.MainActivity.dropIn(MainActivity.java:32)
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:5369)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Application terminated.
据我所知,ImageView
中的图像消失了,现在我点击的是 GridLayout
而不是 ImageView
,这导致它崩溃,因为它不是问题所在我想与之合作。我还认为问题是计数器使所有视图都变成 ImageView
s,但我不确定如何解决这个问题。有任何想法吗?谢谢!
编辑:onCreate 方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView winMessage = findViewById(R.id.winMessage);
winMessage.setText("Let's play! It's Player " + activePlayer + "'s turn.");
}
传递给 dropIn
函数的 view
是一个 GirdLayout
,您正试图将其转换为 ImageView
。
GridLayout counter = (GridLayout) view;
但是,我看到您也在使用相同的函数来处理 ImageView
的 onClick
,但您不能这样做。如果你想使用相同的函数来处理它们,你可以考虑做这样的事情。
public void dropIn(View view) {
if(view.getId() == R.id.gridLayout) {
// Handle action for GridLayout click
} else if(view.getId() == R.id.imageView1) {
// Handle the actions for ImageView click
}
}
我个人认为,您将 onClick
错误地放入了 GridLayout
。所以只需从 GridLayout
xml 中省略 onClick
属性。
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:columnCount="4"
android:rowCount="6"
<!-- android:onClick="dropIn" -->
android:layout_alignParentEnd="true"
android:id="@+id/gridLayout">
您为 GridLayout
和 ImageView
声明了相同的属性 android:onClick="dropIn"
。第一个导致 ClassCastException
在行 ImageView counter = (ImageView) view;
我将 .xml
文件设置为
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:columnCount="4"
android:rowCount="6"
android:onClick="dropIn"
android:layout_alignParentEnd="true"
android:id="@+id/gridLayout">
<ImageView
android:id="@+id/imageView1"
android:layout_width="80dp"
android:layout_height="70dp"
android:layout_column="0"
tools:layout_editor_absoluteX="288dp"
tools:layout_editor_absoluteY="180dp"
android:layout_row="0"
android:onClick="dropIn"
android:contentDescription="@null"
android:tag="1"
app:srcCompat="@drawable/stone" />
无论如何,我有更多的 ImageView,而使它们与众不同的是 ID 和标签。我决定让代码通过标签识别它们。所以这就是我目前拥有的:
int[] gameState = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
public void dropIn(View view) {
ImageView counter = (ImageView) view;
counter.setImageResource(R.drawable.stone);
int tappedCounter = Integer.parseInt(counter.getTag().toString());
try {
if (gameState[tappedCounter] == 2 && gameIsActive) {
gameState[tappedCounter] = 3;
countMe = countMe - 1;
counter.setVisibility(View.INVISIBLE);
} else if (gameState[tappedCounter] == 3 && gameIsActive) {
Toast.makeText(MainActivity.this, "That field is already played!",
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(MainActivity.this, "Oho!",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
这一切工作正常,除了当我点击一个已经被点击但现在不可见的 ImageView
时,它使应用程序崩溃并显示以下消息:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.nedim.lastonestanding, PID: 9758
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:5374)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:5369)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.ClassCastException: android.widget.GridLayout cannot be cast to android.widget.ImageView
at com.nedim.lastonestanding.MainActivity.dropIn(MainActivity.java:32)
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:5369)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Application terminated.
据我所知,ImageView
中的图像消失了,现在我点击的是 GridLayout
而不是 ImageView
,这导致它崩溃,因为它不是问题所在我想与之合作。我还认为问题是计数器使所有视图都变成 ImageView
s,但我不确定如何解决这个问题。有任何想法吗?谢谢!
编辑:onCreate 方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView winMessage = findViewById(R.id.winMessage);
winMessage.setText("Let's play! It's Player " + activePlayer + "'s turn.");
}
传递给 dropIn
函数的 view
是一个 GirdLayout
,您正试图将其转换为 ImageView
。
GridLayout counter = (GridLayout) view;
但是,我看到您也在使用相同的函数来处理 ImageView
的 onClick
,但您不能这样做。如果你想使用相同的函数来处理它们,你可以考虑做这样的事情。
public void dropIn(View view) {
if(view.getId() == R.id.gridLayout) {
// Handle action for GridLayout click
} else if(view.getId() == R.id.imageView1) {
// Handle the actions for ImageView click
}
}
我个人认为,您将 onClick
错误地放入了 GridLayout
。所以只需从 GridLayout
xml 中省略 onClick
属性。
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:columnCount="4"
android:rowCount="6"
<!-- android:onClick="dropIn" -->
android:layout_alignParentEnd="true"
android:id="@+id/gridLayout">
您为 GridLayout
和 ImageView
声明了相同的属性 android:onClick="dropIn"
。第一个导致 ClassCastException
在行 ImageView counter = (ImageView) view;