android - showDialog();错误

时间:2012-05-04 06:01:31

标签: android

我试图写if if条件的wifi连接,但它抛出错误。

logcat的

05-04 13:32:51.227: E/WindowManager(4355): Activity com.smartmenu.SplashActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41813bd0 that was originally added here
05-04 13:32:51.227: E/WindowManager(4355): android.view.WindowLeaked: Activity com.smartmenu.SplashActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41813bd0 that was originally added here
05-04 13:32:51.227: E/WindowManager(4355):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:343)
05-04 13:32:51.227: E/WindowManager(4355):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:245)
05-04 13:32:51.227: E/WindowManager(4355):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:193)
05-04 13:32:51.227: E/WindowManager(4355):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:118)
05-04 13:32:51.227: E/WindowManager(4355):  at android.view.Window$LocalWindowManager.addView(Window.java:537)
05-04 13:32:51.227: E/WindowManager(4355):  at android.app.Dialog.show(Dialog.java:274)
05-04 13:32:51.227: E/WindowManager(4355):  at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
05-04 13:32:51.227: E/WindowManager(4355):  at com.smartmenu.SplashActivity.showDialog(SplashActivity.java:205)
05-04 13:32:51.227: E/WindowManager(4355):  at com.smartmenu.SplashActivity.onCreate(SplashActivity.java:93)
05-04 13:32:51.227: E/WindowManager(4355):  at android.app.Activity.performCreate(Activity.java:4465)
05-04 13:32:51.227: E/WindowManager(4355):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-04 13:32:51.227: E/WindowManager(4355):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
05-04 13:32:51.227: E/WindowManager(4355):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
05-04 13:32:51.227: E/WindowManager(4355):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
05-04 13:32:51.227: E/WindowManager(4355):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
05-04 13:32:51.227: E/WindowManager(4355):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-04 13:32:51.227: E/WindowManager(4355):  at android.os.Looper.loop(Looper.java:137)
05-04 13:32:51.227: E/WindowManager(4355):  at android.app.ActivityThread.main(ActivityThread.java:4340)
05-04 13:32:51.227: E/WindowManager(4355):  at java.lang.reflect.Method.invokeNative(Native Method)
05-04 13:32:51.227: E/WindowManager(4355):  at java.lang.reflect.Method.invoke(Method.java:511)
05-04 13:32:51.227: E/WindowManager(4355):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-04 13:32:51.227: E/WindowManager(4355):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-04 13:32:51.227: E/WindowManager(4355):  at dalvik.system.NativeStart.main(Native Method)

代码

  if(str.equals(""))
        {                          
           ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);    
        if (mWifi.isConnected()) {
           new Handler().postDelayed(new Runnable(){
                @Override
                public void run() {
                /* Create an Intent that will start the Menu-Activity.*/ 
                Intent mainIntent = new Intent(SplashActivity.this,MainActivity.class);
                SplashActivity.this.startActivity(mainIntent);
                SplashActivity.this.finish();
                }
            }, SPLASH_DISPLAY_LENGHT);
        }
        else
        {                   
            showDialog();
        }
        }
        else
        {
            ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
            NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);    
            if (mWifi.isConnected()) {                  
                  new Handler().postDelayed(new Runnable(){
                        @Override
                        public void run() {
                        /* Create an Intent that will start the Menu-Activity.*/ 
                        Intent mainIntent = new Intent(SplashActivity.this,MainActivity.class);
                        SplashActivity.this.startActivity(mainIntent);
                        SplashActivity.this.finish();
                        }
                    }, SPLASH_DISPLAY_LENGHT);
                new DownloadFilesTask().execute();
            }
            else
            {                   
                showDialog();
            }

的ShowDialog();

  private void showDialog()
    {
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("Attention");
        dialog.setMessage("No Internet Connection. Please enable the wifi. ");
        dialog.setPositiveButton("OK", new 
                DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, 
                int whichButton) 
                {
                    new Handler().postDelayed(new Runnable(){
                        @Override
                        public void run() {
                        /* Create an Intent that will start the Menu-Activity.*/ 
                        Intent mainIntent = new Intent(SplashActivity.this,MainActivity.class);
                        SplashActivity.this.startActivity(mainIntent);
                        SplashActivity.this.finish();
                        }
                    }, SPLASH_DISPLAY_LENGHT);
                }
            });

        dialog.show();
    }

2 个答案:

答案 0 :(得分:0)

您在退出活动后尝试显示对话框。

解决方案是在退出Activity之前在您创建的Dialog上调用dismiss(),例如在onPause()。

视图引用了它们的父Context(取自构造函数参数)。如果你在不破坏对话框和其他动态创建的视图的情况下保留一个Activity,它们仍然保持对你的Activity的引用(如果你用它创建了Context作为新的ProgressDialog(this)),那么它就不能被GC收集,导致记忆泄漏。

Alex的原始答案为HERE

答案 1 :(得分:0)

尝试使用AsyncTask并在onPostExecute中显示对话框。在onPreExecute中显示你的闪屏。