我创建了一个包含网格布局和线性布局的项目。网格布局中有一些ImageView。首先,我将线性布局设置为不可见,并将网格布局设置为可见。在这些ImageViews下,添加onClick方法。当我单击这些图像时,onClick方法会将网格布局更改为不可见,而将线性布局更改为可见。因此线性布局出现了,网格布局消失了。但是,每次将网格布局更改为不可见时,App崩溃并给我一个例外:
ClassNotFoundException: Didn't find class
"android.view.View$OnUnhandledKeyEventListener"
但可以将线性布局更改为可见。
我已经搜索过Google并检查了我的代码,但找不到任何问题。
我一步一步检查,发现崩溃仅是由于将网格布局更改为不可见而引起的。我还创建了一个新的简单项目对其进行测试,但仍然存在此问题。
GridLayout mainLayout = (GridLayout)findViewById(R.id.gridLayout);
mainLayout.setVisibility(View.INVISIBLE);
LinearLayout winnerLayout =
(LinearLayout)findViewById(R.id.winnerLayout);
winnerLayout.setVisibility(View.VISIBLE);
XML:
?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.GridLayout
android:id="@+id/gridLayout"
android:layout_width="0dp"
android:layout_height="413dp"
android:background="@drawable/board"
android:visibility="visible"
app:columnCount="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="w,1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:rowCount="3"
>
<ImageView
android:id="@+id/imageView1"
android:layout_width="134dp"
android:layout_height="125dp"
android:layout_marginTop="0dp"
android:onClick="dropIn"
android:scaleType="fitCenter"
android:tag="0"
app:layout_column="0"
app:layout_row="0"
tools:layout_editor_absoluteX="180dp"
tools:layout_editor_absoluteY="162dp" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="134dp"
android:layout_height="125dp"
android:layout_marginLeft="7dp"
android:onClick="dropIn"
android:scaleType="fitCenter"
android:tag="1"
app:layout_column="1"
app:layout_row="0"
tools:layout_editor_absoluteX="180dp"
tools:layout_editor_absoluteY="162dp" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="134dp"
android:layout_height="125dp"
android:layout_marginLeft="4dp"
android:onClick="dropIn"
android:scaleType="fitCenter"
android:tag="2"
app:layout_column="2"
app:layout_row="0"
tools:layout_editor_absoluteX="180dp"
tools:layout_editor_absoluteY="162dp" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="134dp"
android:layout_height="125dp"
android:layout_marginTop="20dp"
android:onClick="dropIn"
android:scaleType="fitCenter"
android:tag="3"
app:layout_column="0"
app:layout_row="1"
tools:layout_editor_absoluteX="180dp"
tools:layout_editor_absoluteY="162dp" />
<ImageView
android:id="@+id/imageView5"
android:layout_width="134dp"
android:layout_height="125dp"
android:layout_marginLeft="7dp"
android:layout_marginTop="20dp"
android:onClick="dropIn"
android:scaleType="fitCenter"
android:tag="4"
app:layout_column="1"
app:layout_row="1"
tools:layout_editor_absoluteX="180dp"
tools:layout_editor_absoluteY="162dp" />
<ImageView
android:id="@+id/imageView6"
android:layout_width="134dp"
android:layout_height="125dp"
android:layout_marginLeft="4dp"
android:layout_marginTop="20dp"
android:onClick="dropIn"
android:scaleType="fitCenter"
android:tag="5"
app:layout_column="2"
app:layout_row="1"
tools:layout_editor_absoluteX="180dp"
tools:layout_editor_absoluteY="162dp" />
<ImageView
android:id="@+id/imageView7"
android:layout_width="134dp"
android:layout_height="125dp"
android:layout_marginTop="15dp"
android:onClick="dropIn"
android:scaleType="fitCenter"
android:tag="6"
app:layout_column="0"
app:layout_row="2"
tools:layout_editor_absoluteX="180dp"
tools:layout_editor_absoluteY="162dp" />
<ImageView
android:id="@+id/imageView8"
android:layout_width="134dp"
android:layout_height="125dp"
android:layout_marginLeft="7dp"
android:layout_marginTop="15dp"
android:onClick="dropIn"
android:scaleType="fitCenter"
android:tag="7"
app:layout_column="1"
app:layout_row="2"
tools:layout_editor_absoluteX="180dp"
tools:layout_editor_absoluteY="162dp" />
<ImageView
android:id="@+id/imageView9"
android:layout_width="134dp"
android:layout_height="125dp"
android:layout_marginLeft="4dp"
android:layout_marginTop="15dp"
android:onClick="dropIn"
android:scaleType="fitCenter"
android:tag="8"
app:layout_column="2"
app:layout_row="2"
tools:layout_editor_absoluteX="180dp"
tools:layout_editor_absoluteY="162dp" />
</android.support.v7.widget.GridLayout>
<LinearLayout
android:id="@+id/winnerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:visibility="invisible"
>
<TextView
android:id="@+id/winnerText"
android:layout_width="310dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:fontFamily="@font/finger_paint"
android:gravity="center"
android:text="Yellow Won"
android:textAlignment="center"
android:textColor="@android:color/holo_red_light"
android:textSize="70sp"
android:typeface="monospace"
tools:layout_editor_absoluteY="222dp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Logcat:
--------- beginning of crash
2019-01-28 12:05:15.704 5002-5002/sailorsinbad.com.udemy5_clock_rotation
E/AndroidRuntime: FATAL EXCEPTION: main
Process: sailorsinbad.com.udemy5_clock_rotation, PID: 5002
java.lang.IllegalStateException: Could not execute method for
android:onClick
at
android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(
AppCompatViewInflater.java:389)
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.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(
AppCompatViewInflater.java:384)
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.support.v7.widget.GridLayout cannot be cast to android.widget.GridLayout
at sailorsinbad.com.udemy5_clock_rotation.MainActivity.resetAndNextGame(MainActivity.java:36)
at sailorsinbad.com.udemy5_clock_rotation.MainActivity.dropIn(MainActivity.java:72)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384)
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)
--------- beginning of system
更新
我点击此链接(android/view/view$onUnhandledKeyEventListener (onMeasure error)),将sdk从28更改为27:
compileSdkVersion 27
targetSdkVersion 27
implementation 'com.android.support:appcompat-v7:27.1.1'
我同步了Gradle并运行它,但仍然崩溃。 我的MainActivity代码:
package sailorsinbad.com.udemy5_clock_rotation;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
boolean winningState = false;
int isRed = 1;
//red is 1; yellow is 2
int[] gameState = {0,0,0,0,0,0,0,0,0};
int[][] winningCondition = {
{0,1,2}, {3,4,5}, {6,7,8},
{0,3,6}, {1,4,7}, {2,5,8},
{0,4,8}, {2,4,6}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void animationDropIn(View drop) {
drop.setTranslationY(-1000f);
drop.animate().translationYBy(1000f).rotation(720).setDuration(250);
}
public void resetAndNextGame(String winner){
//switch to winner layout
GridLayout mainLayout = (GridLayout)findViewById(R.id.gridLayout);
mainLayout.setVisibility(View.INVISIBLE);
LinearLayout winnerLayout =
(LinearLayout)findViewById(R.id.winnerLayout);
winnerLayout.setVisibility(View.VISIBLE);
TextView text = (TextView)findViewById(R.id.winnerText);
text.setText(winner);
}
public void dropIn(View view) {
//catch current rectangle building block area (View)
ImageView drop = (ImageView) view;
//drop-in animation
//2 sides cross drop in, prevent repeat drop, record game state
int tagNum= Integer.parseInt(drop.getTag().toString());
//System.out.println(tagNum);
if (!winningState) {
if (isRed == 1 && gameState[tagNum] == 0) {
drop.setImageResource(R.drawable.red);
gameState[tagNum] = isRed;
animationDropIn(drop);
isRed = 2;
} else if (isRed == 2 && gameState[tagNum] == 0) {
drop.setImageResource(R.drawable.yellow);
gameState[tagNum] = isRed;
animationDropIn(drop);
isRed = 1;
}
// System.out.println(gameState[tagNum]);
//game checking winning condition
for (int[] winniningPosition : winningCondition) {
if (gameState[winniningPosition[0]] ==
gameState[winniningPosition[1]]
&& gameState[winniningPosition[1]] ==
gameState[winniningPosition[2]] &&
gameState[winniningPosition[0]] != 0) {
if (isRed == 2) {
//reversed before
String win = "Red Win";
winningState = true;
resetAndNextGame(win);
} else {
String win = "Yellow Win";
winningState = true;
resetAndNextGame(win);
}
} else {
boolean isGameOver = true;
for (int counter : gameState) {
if (counter == 0) {
isGameOver = false;
}
}
if (isGameOver) {
String win = "Draw";
winningState = true;
resetAndNextGame(win);
}
}
}
}
}
}
我的构建。Gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "sailorsinbad.com.udemy5_clock_rotation"
minSdkVersion 21
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
}
dependencies {
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-
core:3.0.2'
implementation 'com.android.support:gridlayout-v7:27.1.1'
}
答案 0 :(得分:0)
您的MainActivity import android.widget.GridLayout;
中的导入错误。
您需要使用import android.support.v7.widget.GridLayout
,因为您要在xml文件中夸大GridLayout的支持库版本。
查看崩溃原因-Caused by: java.lang.ClassCastException: android.support.v7.widget.GridLayout cannot be cast to android.widget.GridLayout