类似的代码不会创建错误时发生IllegalStateException

时间:2013-05-21 19:14:41

标签: java android illegalstateexception

所以我写了两个代码片段,一个按预期工作,另一个生成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)];
}

}

logcat的:

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

试试这个:

    Intent intent = new Intent(this, g.getRandomScreen());
    while (intent==null);
    startActivity(intent);