您好,我只是想问为什么我无法启动我的程序,当我开始使用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;
}
}
};
}
和 logcat
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
答案 0 :(得分: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);
}
我想我现在看到了这个错误。您正在尝试在调用onCreate
之前调用View.OnClickListener:
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(){
...
};
}
}
这允许在尝试设置侦听器之前创建活动。
更好的是,你可能是applying this onClickListener to the 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();
}
}
答案 1 :(得分:0)
为什么不在活动上实现onClickListener。在您的代码中,您在调用onCreate之前创建了对de活动上下文的引用,因此上下文不可用。点netinept在类中声明属性然后在constuctor / methode中实例化它们也是一件好事。在Android中,您通常不必为活动或片段使用构造函数,因为存在被调用的生命周期方法。有关详细信息,请参阅Android文档Activity Lifecycle。
如果在创建activity / fragment之前确实需要上下文,则需要在Application类包装器中创建一个返回Application上下文的静态方法。如果没有正确使用,这可能会导致泄漏,所以只有在你知道自己在做什么的情况下才会这样做。
下面一个应该更好的代码示例。
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;
}
}
}