gpt4 book ai didi

java - 当我将布局更改为不可见时如何修复 ClassNotFoundException

转载 作者:行者123 更新时间:2023-11-30 05:07:05 24 4
gpt4 key购买 nike

我创建了一个包含网格布局和线性布局的项目。网格布局中有一些ImageView。首先,我将线性布局设置为不可见,并将网格布局设置为可见。在这些 ImageViews 下,我添加了 onClick 方法。当我单击这些图像时,onClick 方法会将网格布局更改为不可见,将线性布局更改为可见。所以线性布局出来了,网格布局消失了。但是,每次它将网格布局更改为不可见时,应用程序崩溃并给我一个异常(exception):

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>

日志:

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

Design View

Code

更新

我点击此链接 ( android/view/view$onUnhandledKeyEventListener (onMeasure error) ) 将 sdk 从 28 更改为 27,如下所示:

compileSdkVersion 27

targetSdkVersion 27

implementation 'com.android.support:appcompat-v7:27.1.1'

我同步了 Gradle 并运行它,但仍然崩溃。我的主要 Activity 代码:

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);
}

}

}
}
}
}

我的build.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'
}

最佳答案

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

关于java - 当我将布局更改为不可见时如何修复 ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54385607/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com