gpt4 book ai didi

java - 当类似的代码不会产生错误时发生 IllegalStateException

转载 作者:行者123 更新时间:2023-12-01 14:33:36 26 4
gpt4 key购买 nike

所以我写了两个代码 fragment ,一个按预期工作,另一个生成 IllegalStateException 错误,而在我看来,它们应该做完全相同的事情。为什么我使用 GameScreen 对象时会出现错误。 GameScreen 对象代码应返回与工作代码相同的内容。所以我不知道为什么这不起作用。

工作代码:

public void begin(View view) {

Class<?>[] cls = new Class<?>[5];
cls[0]=QuickTouchActivity.class;
cls[1]=LongTouchActivity.class;
cls[2]=MiddleTouchActivity.class;
cls[3]=LeftTouchActivity.class;
cls[4]=RightTouchActivity.class;
Random r= new Random();


Intent intent = new Intent(this, cls[r.nextInt(cls.length)]);
startActivity(intent);



}

错误代码:

public void begin(View view) {


GameScreen g = new GameScreen();
Intent intent = new Intent(this, g.getRandomScreen());
startActivity(intent);
}
import java.util.Random;

public class GameScreen
{
private Class<?>[] cls;

public GameScreen()
{
cls= new Class<?>[5];
Class<?>[] cls = new Class<?>[5];
cls[0]=QuickTouchActivity.class;
cls[1]=LongTouchActivity.class;
cls[2]=MiddleTouchActivity.class;
cls[3]=LeftTouchActivity.class;
cls[4]=RightTouchActivity.class;
}

public Class<?> getRandomScreen()
{
Random r= new Random();
return cls[r.nextInt(cls.length)];
}

}

日志猫:

05-21 19:09:03.849: E/AndroidRuntime(361): FATAL EXCEPTION: main
05-21 19:09:03.849: E/AndroidRuntime(361): java.lang.IllegalStateException: Could not execute method of the activity
05-21 19:09:03.849: E/AndroidRuntime(361): at android.view.View$1.onClick(View.java:2704)
05-21 19:09:03.849: E/AndroidRuntime(361): at android.view.View.performClick(View.java:3100)
05-21 19:09:03.849: E/AndroidRuntime(361): at android.view.View$PerformClick.run(View.java:11644)
05-21 19:09:03.849: E/AndroidRuntime(361): at android.os.Handler.handleCallback(Handler.java:587)
05-21 19:09:03.849: E/AndroidRuntime(361): at android.os.Handler.dispatchMessage(Handler.java:92)
05-21 19:09:03.849: E/AndroidRuntime(361): at android.os.Looper.loop(Looper.java:126)
05-21 19:09:03.849: E/AndroidRuntime(361): at android.app.ActivityThread.main(ActivityThread.java:3997)
05-21 19:09:03.849: E/AndroidRuntime(361): at java.lang.reflect.Method.invokeNative(Native Method)
05-21 19:09:03.849: E/AndroidRuntime(361): at java.lang.reflect.Method.invoke(Method.java:491)
05-21 19:09:03.849: E/AndroidRuntime(361): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
05-21 19:09:03.849: E/AndroidRuntime(361): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
05-21 19:09:03.849: E/AndroidRuntime(361): at dalvik.system.NativeStart.main(Native Method)
05-21 19:09:03.849: E/AndroidRuntime(361): Caused by: java.lang.reflect.InvocationTargetException
05-21 19:09:03.849: E/AndroidRuntime(361): at java.lang.reflect.Method.invokeNative(Native Method)
05-21 19:09:03.849: E/AndroidRuntime(361): at java.lang.reflect.Method.invoke(Method.java:491)
05-21 19:09:03.849: E/AndroidRuntime(361): at android.view.View$1.onClick(View.java:2699)
05-21 19:09:03.849: E/AndroidRuntime(361): ... 11 more
05-21 19:09:03.849: E/AndroidRuntime(361): Caused by: java.lang.NullPointerException
05-21 19:09:03.849: E/AndroidRuntime(361): at android.content.ComponentName.<init>(ComponentName.java:76)
05-21 19:09:03.849: E/AndroidRuntime(361): at android.content.Intent.<init>(Intent.java:2840)
05-21 19:09:03.849: E/AndroidRuntime(361): at com.example.worldshardestgame.Home.begin(Home.java:41)
05-21 19:09:03.849: E/AndroidRuntime(361): ... 14 more

最佳答案

是因为您在游戏屏幕构造函数中声明了两个不同的 cls 变量吗?只需一个实例字段就足够了。我不知道为什么同时删除两者会产生非法状态异常,但这也许是您的问题。

关于java - 当类似的代码不会产生错误时发生 IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16677751/

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