0

嗨,我只想问为什么我无法启动我的程序,当我从 MessageBox 或 AlertBox 启动时它总是显示错误。我想要做的是,如果玩家在初学者阶段没有回答超过 15 个问题,他就不能进入学习者阶段,当他点击学习者时,它会显示一个消息框,他需要回答 10+能够进入下一轮的答案是我的代码

public class Menu extends Activity 
{

private Integer beginnerAns = 0;


Button beginner, learner;

@Override
protected void onCreate(Bundle MenuButtons) 
{
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(MenuButtons);
    setContentView(R.layout.menu);

    beginner = (Button) findViewById(R.id.btnBeginner);
    learner = (Button) findViewById(R.id.btnLearner);

    beginner.setOnClickListener(myOnlyhandler);
    learner.setOnClickListener(myOnlyhandler);

}

View.OnClickListener myOnlyhandler = new View.OnClickListener(){
public AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(Menu.this);

@Override
public void onClick(View v) 
{
    switch(v.getId()) {
    case R.id.btnBeginner:
        Intent openBeginner = new Intent("com.thesis.logipic.BEGINNER");
        startActivity(openBeginner);
    break;
    case R.id.btnLearner:
        if (beginnerAns < 15)
        {
            dlgAlert.setMessage("wrong password or username");
            dlgAlert.setTitle("Error Message...");
            dlgAlert.setPositiveButton("OK", null);
            dlgAlert.setCancelable(true);
            dlgAlert.create().show();
        }
        else
        {
        Intent openLearner = new Intent("com.thesis.logipic.GAMEPLAY");
        startActivity(openLearner);
        }
    break;
    }
}
};

}

日志猫

10-06 21:00:28.673: D/AndroidRuntime(561): Shutting down VM
10-06 21:00:28.692: W/dalvikvm(561): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-06 21:00:28.712: E/AndroidRuntime(561): FATAL EXCEPTION: main
10-06 21:00:28.712: E/AndroidRuntime(561): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.thesis.logipic/com.thesis.logipic.Menu}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
10-06 21:00:28.712: E/AndroidRuntime(561):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
10-06 21:00:28.712: E/AndroidRuntime(561):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-06 21:00:28.712: E/AndroidRuntime(561):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-06 21:00:28.712: E/AndroidRuntime(561):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-06 21:00:28.712: E/AndroidRuntime(561):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-06 21:00:28.712: E/AndroidRuntime(561):  at android.os.Looper.loop(Looper.java:123)
10-06 21:00:28.712: E/AndroidRuntime(561):  at android.app.ActivityThread.main(ActivityThread.java:4627)
10-06 21:00:28.712: E/AndroidRuntime(561):  at java.lang.reflect.Method.invokeNative(Native Method)
10-06 21:00:28.712: E/AndroidRuntime(561):  at java.lang.reflect.Method.invoke(Method.java:521)
10-06 21:00:28.712: E/AndroidRuntime(561):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-06 21:00:28.712: E/AndroidRuntime(561):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-06 21:00:28.712: E/AndroidRuntime(561):  at dalvik.system.NativeStart.main(Native Method)
10-06 21:00:28.712: E/AndroidRuntime(561): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
10-06 21:00:28.712: E/AndroidRuntime(561):  at android.app.Activity.getSystemService(Activity.java:3526)
10-06 21:00:28.712: E/AndroidRuntime(561):  at com.android.internal.app.AlertController$AlertParams.<init>(AlertController.java:743)
10-06 21:00:28.712: E/AndroidRuntime(561):  at android.app.AlertDialog$Builder.<init>(AlertDialog.java:273)
10-06 21:00:28.712: E/AndroidRuntime(561):  at com.thesis.logipic.Menu$1.<init>(Menu.java:38)
10-06 21:00:28.712: E/AndroidRuntime(561):  at com.thesis.logipic.Menu.<init>(Menu.java:37)
10-06 21:00:28.712: E/AndroidRuntime(561):  at java.lang.Class.newInstanceImpl(Native Method)
10-06 21:00:28.712: E/AndroidRuntime(561):  at java.lang.Class.newInstance(Class.java:1429)
10-06 21:00:28.712: E/AndroidRuntime(561):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
10-06 21:00:28.712: E/AndroidRuntime(561):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
10-06 21:00:28.712: E/AndroidRuntime(561):  ... 11 more
4

2 回答 2

1

你的电话super必须是你做的第一件事。

@Override
protected void onCreate(Bundle MenuButtons) 
{
    super.onCreate(MenuButtons);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.menu);

    beginner = (Button) findViewById(R.id.btnBeginner);
    learner = (Button) findViewById(R.id.btnLearner);

    beginner.setOnClickListener(myOnlyhandler);
    learner.setOnClickListener(myOnlyhandler);

}

编辑:

我想我现在看到了错误。您正在尝试在调用 View.OnClickListener 之前onCreate调用:

public class Menu extends Activity 
{

    ...

    @Override
    protected void onCreate(Bundle MenuButtons) 
    {
        super.onCreate(MenuButtons);
        ...

    }

    View.OnClickListener myOnlyhandler = new View.OnClickListener(){
         ...
    };

}

相反,您应该在 onCreate 中进行调用:

public class Menu extends Activity 
{

    ...

    @Override
    protected void onCreate(Bundle MenuButtons) 
    {
        super.onCreate(MenuButtons);
        ...

        View.OnClickListener myOnlyhandler = new View.OnClickListener(){
             ...
        };

    }


}

这允许在您尝试设置侦听器之前创建活动。

更好的是,您可以将此 onClickListener 应用于 Activity

public class Menu extends Activity 
{

    ...

    @Override
    protected void onCreate(Bundle MenuButtons) 
    {
        super.onCreate(MenuButtons);
        ...
    }

    @Override
    public void onClick(View v)
    {
        ...
    };

}

导致我这样做的提示是堆栈跟踪行:

Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()

您可能会请求哪些系统服务?View.OnClickListener!

作为一般规则,当涉及到 Java 时,您不希望在任何方法或构造函数之外实例化项目。例如:

class A {
    B b = new B();

    A(){

    }
}

您可能希望在构造函数中实例化项目:

class A {
    B b;

    A(){
        b = new B();
    }
}
于 2013-10-06T13:18:20.573 回答
0

为什么不在活动上实现 onClickListener。在您的代码中,您在调用 onCreate 之前创建对 de 活动上下文的引用,因此上下文不可用。netinept 在类中声明属性然后在构造函数/方法中实例化它们也是一件好事。在 Android 中,您通常不必为活动或片段使用构造函数,因为会调用生命周期方法。有关更多信息Activity Lifecycle,请参阅 android 文档。

如果您在创建活动/片段之前确实需要一个上下文,则需要在您的应用程序类包装器中创建一个返回应用程序上下文的静态方法。如果使用不当,这可能会导致泄漏,因此只有在您知道自己在做什么的情况下才这样做。

下面的代码示例应该会更好。

public class Menu extends Activity implements onClickListener {
   public AlertDialog.Builder dlgAlert;

   public void onCreat() {
       super.onCreate(MenuButtons);
       ....
       beginner.setOnClickListener(this);
       learner.setOnClickListener(this);
       dlgAlert  = new AlertDialog.Builder(this);
   }

   @Override
   public void onClick(View v) {
       switch(v.getId()) {
       case R.id.btnBeginner:
           Intent openBeginner = new Intent("com.thesis.logipic.BEGINNER");
           startActivity(openBeginner);
       break;
       case R.id.btnLearner:
           if (beginnerAns < 15) {
               dlgAlert.setMessage("wrong password or username");
               dlgAlert.setTitle("Error Message...");
               dlgAlert.setPositiveButton("OK", null);
               dlgAlert.setCancelable(true);
               dlgAlert.create().show();
           } else {
               Intent openLearner = new Intent("com.thesis.logipic.GAMEPLAY");
               startActivity(openLearner);
           }
       break;
       }
    }
}
于 2013-10-06T13:44:38.793 回答