在处理变量的Activity中放置onPause

时间:2013-10-13 01:03:20

标签: android

我需要帮助将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下出现语法错误,该错误表示“令牌上的语法错误”,“@”,“我希望”放在onStop的底部,它无法识别alertDialog我不知道如何解决这个问题,我已经在这里工作了几个小时,并且知道如何解决它。所以可以帮助我将onPause放在我活动的适当位置

logcat的

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)

2 个答案:

答案 0 :(得分:0)

我认为你有to read about OOP first,知道什么是方法,变量,字段以及代码中每个元素的语法是什么。

让我们回到你的问题:

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

这又是非常正常的,你已经将alertDialog声明为局部变量,这意味着你不会从那个方法中得到它。 :Variable Scopes in Java

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

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

/*  .... */

@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的方法。 Read this to understand what does it do !,并按照以下方式更正onPause

@Override 
public void onPause() { 
    super.onPause();  // Always call the superclass method first
    if (alertDialog != null && alertDialog.isShowing()) { 
        alertDialog.dismiss(); super.onPause(); 
    } 
} 

答案 1 :(得分:0)

应该是

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