0

我需要一些帮助来放置onPause我的代码。这是我onPause今天早些时候建议的方法

@Override public void onPause() { 
if (alertDialog != null && alertDialog.isShowing()) { 
alertDialog.dismiss(); super.onPause(); } } 

我的活动

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

 final SharedPreferences settings = getSharedPreferences("pref_name", 0);
    boolean disappear = settings.getBoolean("installed", false);

    if (disappear == false) {

        final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

        alertDialog.setIcon(R.drawable.ic_launcher);
        alertDialog.setTitle("Title");
        alertDialog.setMessage("dialog");

        alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
            settings.edit().putBoolean("installed", true).commit();

                }
            });

 AlertDialog alertDialog = alertDialogBuilder.create();
 alertDialog.show();   



            final EditText editText = (EditText) findViewById(R.id.typo1);
            Button check = (Button) findViewById(R.id.check1);
            check.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {       
                if (editText.getText().toString().length()==0) {
                    Toast.makeText(getApplicationContext(),"Congrats!",Toast.LENGTH_LONG).show();               


    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
    }

}

当我尝试将其放在onPause下面时,alertDialog.show();我得到一个语法错误void,上面写着“令牌“void”上的语法错误,@预期”,当我尝试将其放在底部时,onStop它无法识别alertDialog我不知道如何解决这个问题问题,我已经解决了几个小时并且知道如何解决它。所以可以帮助我onPause在我的活动中放置适当的位置

日志猫

10-13 03:10:37.496: E/AndroidRuntime(761): FATAL EXCEPTION: main
10-13 03:10:37.496: E/AndroidRuntime(761): android.app.SuperNotCalledException: Activity {com.theproblemsolver/com.theproblemsolver.MainActivity} did not call through to super.onPause()
10-13 03:10:37.496: E/AndroidRuntime(761):  at android.app.Activity.performPause(Activity.java:5210)
10-13 03:10:37.496: E/AndroidRuntime(761):  at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1226)
10-13 03:10:37.496: E/AndroidRuntime(761):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3002)
10-13 03:10:37.496: E/AndroidRuntime(761):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2971)
10-13 03:10:37.496: E/AndroidRuntime(761):  at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2949)
10-13 03:10:37.496: E/AndroidRuntime(761):  at android.app.ActivityThread.access$800(ActivityThread.java:141)
10-13 03:10:37.496: E/AndroidRuntime(761):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1245)
10-13 03:10:37.496: E/AndroidRuntime(761):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-13 03:10:37.496: E/AndroidRuntime(761):  at android.os.Looper.loop(Looper.java:137)
10-13 03:10:37.496: E/AndroidRuntime(761):  at android.app.ActivityThread.main(ActivityThread.java:5041)
10-13 03:10:37.496: E/AndroidRuntime(761):  at java.lang.reflect.Method.invokeNative(Native Method)
10-13 03:10:37.496: E/AndroidRuntime(761):  at java.lang.reflect.Method.invoke(Method.java:511)
10-13 03:10:37.496: E/AndroidRuntime(761):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-13 03:10:37.496: E/AndroidRuntime(761):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-13 03:10:37.496: E/AndroidRuntime(761):  at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

0

我认为您必须首先阅读 OOP,了解什么是方法、变量、字段,以及代码中每个元素的语法是什么。

让我们回到你的问题:

When I try to put my onPause under alertDialog.show(); I get an syntax error under void that says "syntax error on token "void"

这很正常,正如你所说,这是一个语法错误,这意味着你没有尊重定义方法的正常语法:Defining methods in java

when I try to put at the bottom with onStop it doesn't recognize alertDialog

这又很正常,您已将 your 声明alertDialog为局部变量,这意味着您不会从该方法中获取它。: Java 中的变量作用域

So can help me place onPause in the proper place in my activity

您可以将它放在该类中的任何其他方法之后/之前,例如,在您的 on stop 之后:

/*  .... */

@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
}

@Override 
public void onPause() { 
    if (alertDialog != null && alertDialog.isShowing()) { 
        alertDialog.dismiss(); super.onPause(); 
    } 
} 

编辑 :

您的日志的这一行:

android.app.SuperNotCalledException: Activity {com.theproblemsolver/com.theproblemsolver.MainActivity} did not call through to super.onPause()

意味着您必须调用super()在您的onPause. 阅读本文以了解它的作用!,并纠正你onPause这样的:

@Override 
public void onPause() { 
    super.onPause();  // Always call the superclass method first
    if (alertDialog != null && alertDialog.isShowing()) { 
        alertDialog.dismiss(); super.onPause(); 
    } 
} 
于 2013-10-13T01:16:37.363 回答
0

它应该像

@Override public void onPause() { 
          super.onPause();
          if (alertDialog != null && alertDialog.isShowing()) 
          { 
           alertDialog.dismiss();  
          } } 
于 2013-10-14T06:59:04.040 回答