0

所以我写了两个代码片段,一个按预期工作,另一个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
4

1 回答 1

1

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

于 2013-05-21T19:53:50.330 回答