与其他类完成活动时出错

时间:2015-01-27 02:41:07

标签: java android android-activity

当我尝试通过另一个类终止应用程序时,有人可以解释为什么错误发生在问题结束时列出,关闭应用程序:

classe1.class

    ...
    ...
    AlertDialogManager alert = new AlertDialogManager();
    NetworkAvailable internet = new NetworkAvailable();

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

    if (!internet.isNetworkAvailable(this)){
    alert.showAlertDialog(MainActivity.this, "No network!", false);

    if (alert.Saindo()){

       //ERROR HAPPENS THE FINISH (), I WANTED THAT THIS FUNCTION execute
       //AFTER I cancel THERE IN class3.
       //The app NOT START, if I let this finish here, but if I withdraw,
       //The LOG SPEAK:

       Log("Finished", "Closing the application!")
            finish();

        }

    }

}




  }

classe2.class

public class NetworkAvailable{

        public boolean isNetworkAvailable (final Context context) {
            ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = manager.getActiveNetworkInfo();


            boolean isAvailable = false;
            if(networkInfo != null && networkInfo.isConnected()){

                Toast.makeText(context, "CONECTED", Toast.LENGTH_LONG).show();
                isAvailable = true;
            } else {

                Toast.makeText(context, "NO CONECTED", Toast.LENGTH_LONG).show();
                isAvailable = false;
            }
            return isAvailable;

        }
}

classe3.class

public class AlertDialogManager {

    public boolean showAlertDialog(final Context context, final String title, final Boolean status) {


    if(status == false){
     AlertDialog.Builder builder = new AlertDialog.Builder(context);
     builder.setCancelable(false);
     builder.setTitle(title);

     builder.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface dialog, int which) {                       


             Log.v("CANCELANDO", "cancelado");
             showAlertDialog(context, title, true);
             Saindo();

         }
     });

     builder.setPositiveButton("Tentar novamente", new DialogInterface.OnClickListener(){
         @Override
         public void onClick(DialogInterface dialog, int which)
         {

            final ConnectivityManager conectivtyManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);  
            if (conectivtyManager.getActiveNetworkInfo() != null  
                    && conectivtyManager.getActiveNetworkInfo().isAvailable()  
                    && conectivtyManager.getActiveNetworkInfo().isConnected()) {  


            } 
             dialog.dismiss();
             showAlertDialog(context, title, false);
         }
     });
     AlertDialog dialog = builder.create(); // calling builder.create after adding buttons
     dialog.show();
    return status;
    }
    return status;  
    }

    public boolean Saindo(){

        Log.v("SAINDO", "saiu");        
        return true;

    }



}

01-26 22:17:05.721: E/WindowManager(25592): Activity ffitteste.favoritos.mapa.rafael.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@418654f0 that was originally added here
01-26 22:17:05.721: E/WindowManager(25592): android.view.WindowLeaked: Activity ffitteste.favoritos.mapa.rafael.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@418654f0 that was originally added here
01-26 22:17:05.721: E/WindowManager(25592):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:351)
01-26 22:17:05.721: E/WindowManager(25592):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:279)
01-26 22:17:05.721: E/WindowManager(25592):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
01-26 22:17:05.721: E/WindowManager(25592):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
01-26 22:17:05.721: E/WindowManager(25592):     at android.view.Window$LocalWindowManager.addView(Window.java:537)
01-26 22:17:05.721: E/WindowManager(25592):     at android.app.Dialog.show(Dialog.java:278)
01-26 22:17:05.721: E/WindowManager(25592):     at ffitteste.favoritos.mapa.rafael.AlertDialogManager.showAlertDialog(AlertDialogManager.java:48)
01-26 22:17:05.721: E/WindowManager(25592):     at ffitteste.favoritos.mapa.rafael.MainActivity.onCreate(MainActivity.java:103)
01-26 22:17:05.721: E/WindowManager(25592):     at android.app.Activity.performCreate(Activity.java:4465)
01-26 22:17:05.721: E/WindowManager(25592):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
01-26 22:17:05.721: E/WindowManager(25592):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
01-26 22:17:05.721: E/WindowManager(25592):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
01-26 22:17:05.721: E/WindowManager(25592):     at android.app.ActivityThread.access$600(ActivityThread.java:128)
01-26 22:17:05.721: E/WindowManager(25592):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
01-26 22:17:05.721: E/WindowManager(25592):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-26 22:17:05.721: E/WindowManager(25592):     at android.os.Looper.loop(Looper.java:137)
01-26 22:17:05.721: E/WindowManager(25592):     at android.app.ActivityThread.main(ActivityThread.java:4514)
01-26 22:17:05.721: E/WindowManager(25592):     at java.lang.reflect.Method.invokeNative(Native Method)
01-26 22:17:05.721: E/WindowManager(25592):     at java.lang.reflect.Method.invoke(Method.java:511)
01-26 22:17:05.721: E/WindowManager(25592):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
01-26 22:17:05.721: E/WindowManager(25592):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
01-26 22:17:05.721: E/WindowManager(25592):     at dalvik.system.NativeStart.main(Native Method)
01-26 22:17:05.729: E/WindowManager(25592): Activity ffitteste.favoritos.mapa.rafael.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41776208 that was originally added here
01-26 22:17:05.729: E/WindowManager(25592): android.view.WindowLeaked: Activity ffitteste.favoritos.mapa.rafael.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41776208 that was originally added here
01-26 22:17:05.729: E/WindowManager(25592):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:351)
01-26 22:17:05.729: E/WindowManager(25592):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:279)
01-26 22:17:05.729: E/WindowManager(25592):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
01-26 22:17:05.729: E/WindowManager(25592):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
01-26 22:17:05.729: E/WindowManager(25592):     at android.view.Window$LocalWindowManager.addView(Window.java:537)
01-26 22:17:05.729: E/WindowManager(25592):     at android.app.Dialog.show(Dialog.java:278)
01-26 22:17:05.729: E/WindowManager(25592):     at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
01-26 22:17:05.729: E/WindowManager(25592):     at ffiteteste.favoritos.servicos.rafael.GPSTracker.showSettingsAlert(GPSTracker.java:194)
01-26 22:17:05.729: E/WindowManager(25592):     at ffitteste.favoritos.mapa.rafael.MainActivity.GPSzoom(MainActivity.java:819)
01-26 22:17:05.729: E/WindowManager(25592):     at ffitteste.favoritos.mapa.rafael.MainActivity.onCreate(MainActivity.java:164)
01-26 22:17:05.729: E/WindowManager(25592):     at android.app.Activity.performCreate(Activity.java:4465)
01-26 22:17:05.729: E/WindowManager(25592):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
01-26 22:17:05.729: E/WindowManager(25592):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
01-26 22:17:05.729: E/WindowManager(25592):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
01-26 22:17:05.729: E/WindowManager(25592):     at android.app.ActivityThread.access$600(ActivityThread.java:128)
01-26 22:17:05.729: E/WindowManager(25592):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
01-26 22:17:05.729: E/WindowManager(25592):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-26 22:17:05.729: E/WindowManager(25592):     at android.os.Looper.loop(Looper.java:137)
01-26 22:17:05.729: E/WindowManager(25592):     at android.app.ActivityThread.main(ActivityThread.java:4514)
01-26 22:17:05.729: E/WindowManager(25592):     at java.lang.reflect.Method.invokeNative(Native Method)
01-26 22:17:05.729: E/WindowManager(25592):     at java.lang.reflect.Method.invoke(Method.java:511)
01-26 22:17:05.729: E/WindowManager(25592):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
01-26 22:17:05.729: E/WindowManager(25592):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
01-26 22:17:05.729: E/WindowManager(25592):     at dalvik.system.NativeStart.main(Native Method)

HELPER!

1 个答案:

答案 0 :(得分:0)

由于您未使用代码段发布实际类名称,因此有点不清楚。但看起来好像是因为在完成Dialog之前你没有关闭Activity

if (alert.Saindo()){

如果您将其更改为

if (alert.Saindo()){
    if (alert != null && alert.isShowing()) 
        alert.dismiss();

它应该照顾你的问题。

Activity仍在显示时,您正在完成Dialog