在检查Dialog不等于Null之后,在Dialog DISMISS上出现错误

时间:2016-04-15 05:28:00

标签: android

即使在检查if(dialog!= null和dialog.isShowing())之后,我也收到此错误。有没有人知道它发生的原因是什么。

下面是我的堆栈跟踪和类代码。提前致谢

 STACK_TRACE=java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{2c7550a8 V.E..... R.....ID 0,0-326,175} not attached to window manager
    at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:396)
    at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:322)
    at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:116)
    at android.app.Dialog.dismissDialog(Dialog.java:341)
    at android.app.Dialog.dismiss(Dialog.java:324)
    at com.abcd.org.Eula$9.onPageFinished(Eula.java:332)
    at com.android.webview.chromium.WebViewContentsClientAdapter.onPageFinished(WebViewContentsClientAdapter.java:513)
    at org.chromium.android_webview.AwContentsClientCallbackHelper$MyHandler.handleMessage(AwContentsClientCallbackHelper.java:188)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5343)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

这是课程:

public class Eula {


    private static Boolean dialog_show ;

    static interface OnEulaAgreedTo {
        void onEulaAgreedTo();
    }

    public static boolean show(final Activity activity,final Context context,final Boolean flag) {
        final SharedPreferences preferences = activity.getSharedPreferences(PREFERENCES_EULA, Activity.MODE_PRIVATE);
        Log.d(TAG, "insideEula");
        dialog_show = false;

        final ProgressDialog progress = new ProgressDialog(activity);
        progress.setMessage("Loading EULA......");
        progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progress.setIndeterminate(true);
        progress.setCanceledOnTouchOutside(false);
        progress.setCancelable(false);



        if (!preferences.getBoolean(PREFERENCE_EULA_ACCEPTED, false)) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(
                    activity);
            Log.d(TAG, "insideEulaLaunch");
            builder.setTitle(R.string.eula_title);
            builder.setCancelable(true);
            builder.setPositiveButton(R.string.eula_accept,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            preferences.edit().putBoolean(PREFERENCE_EULA_ACCEPTED, true).commit();
                            if (activity instanceof OnEulaAgreedTo) {
                                ((OnEulaAgreedTo) activity).onEulaAgreedTo();

                            }

                        }
                    });
            builder.setNegativeButton(R.string.eula_refuse,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            refuse(activity);

                        }
                    });
            builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
                public void onCancel(DialogInterface dialog) {
                    refuse(activity);
                }
            });
            WebView wv = new WebView(activity);
              wv.getSettings().setJavaScriptEnabled(true);

                wv.getSettings().setBuiltInZoomControls(true);

                wv.getSettings().setSupportZoom(true);

                wv.getSettings().setUseWideViewPort(true);


          /*      wv.getSettings().getAllowContentAccess();

                wv.getSettings().setJavaScriptEnabled(true);

                wv.getSettings().supportMultipleWindows();

                wv.getSettings().setAllowContentAccess(true);

                wv.getSettings().setAllowFileAccess(true);

                wv.getSettings().setAllowFileAccessFromFileURLs(true);

                wv.getSettings().setAllowUniversalAccessFromFileURLs(true);*/

                wv.setClickable(true);

                wv.setInitialScale(1);

            try{
                wv.loadUrl(context.getString(R.string.eula_url));
            }catch(Exception e){
                Log.d(TAG, "eula fil" + e.getMessage().toString());             
            }       


            wv.setOnKeyListener(new OnKeyListener()
            {
                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event)
                {
                    if(event.getAction() == KeyEvent.ACTION_DOWN)
                    {
                        WebView webView = (WebView) v;

                        switch(keyCode)
                        {
                            case KeyEvent.KEYCODE_BACK:
                                if(webView.canGoBack())
                                {
                                    webView.goBack();
                                    return true;
                                }
                                break;
                        }
                    }

                    return false;
                }
            });
            wv.requestFocus(View.FOCUS_DOWN);
            wv.setOnTouchListener(new View.OnTouchListener()
            {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    // TODO Auto-generated method stub
                    switch (event.getAction())
                    {
                        case MotionEvent.ACTION_DOWN:
                        case MotionEvent.ACTION_UP:
                            if (!v.hasFocus())
                            {
                                v.requestFocus();
                            }
                            break;
                    }
                    return false;
                }
            });
            wv.setWebViewClient(new WebViewClient() {


                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    // view.loadUrl(url);
                    // return true;
                    Log.d(TAG, "shouldOverrideUrlLoading" + url);

                    Pattern pattern = Pattern.compile("[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}");


                    String cleanUrl = url;
                    if (url.contains("?")) {
                        // remove the query string
                        cleanUrl = url.substring(0, url.indexOf("?"));
                    }

                    Matcher mat = pattern.matcher(url);

                    Log.d(TAG, "url == " + url);

                    if (cleanUrl.endsWith("pdf")) {
                        Log.d(TAG, "pdf");
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.parse(url), "application/pdf");
                        try {
                            Uri uriUrl = Uri.parse(cleanUrl);
                            Intent intentUrl = new Intent(Intent.ACTION_VIEW,
                                    uriUrl);
                            view.getContext().startActivity(intentUrl);
                            return true;

                        } catch (Exception e) {
                            Toast.makeText(context, "No PDF Viewer Installed",
                                    Toast.LENGTH_LONG).show();
                        }
                    } else if(url.contains("mailto:")){
                        Log.d(TAG, "mailto");
                        Uri uriUrl = Uri.parse(cleanUrl);
                        Intent intentUrl = new Intent(Intent.ACTION_VIEW,
                                uriUrl);
                        view.getContext().startActivity(intentUrl);
                        return true;
                    } else if(url.contains("tel:")){
                        Log.d(TAG, "tel");
                        String telUrl = url;                        
                        Intent intent = new Intent(Intent.ACTION_DIAL);
                        intent.setData(Uri.parse(telUrl));
                        view.getContext().startActivity(intent);
                    }
                    else {
                        Log.d(TAG, "default");
//                      view.loadUrl(url);
                        Uri uriUrl = Uri.parse(cleanUrl);
                        Intent intentUrl = new Intent(Intent.ACTION_VIEW,
                                uriUrl);
                        view.getContext().startActivity(intentUrl);
                    }
                    return true;
                }

                @Override
                public void onPageFinished(WebView view, String url) {
                    // TODO Auto-generated method stub
                    Log.d(TAG, "onPageFinished");
                    if (progress != null && progress.isShowing()) {
                        progress.dismiss(); 
                        if(DataTransLayer.canTransferData(context)){
                            builder.show();
                        }else{                          
//                          builder.show().getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);                            
                            activity.finish();
                        }
                    }   

                }

                @Override
                public void onPageStarted(WebView view, String url,
                        Bitmap favicon) {
                    // TODO Auto-generated method stub
                    Log.d(TAG, "onPageStarted");
                    if(!dialog_show){
                        progress.show();
                        dialog_show = true;
                    }                   

                }           

            });

            builder.setView(wv);            

            return false;
        }
        return true;
    }



    private static void refuse(Activity activity) {
        activity.finish();
    }

    private static CharSequence readEula(Activity activity) {
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(activity.getAssets()
                    .open(ASSET_EULA)));
            String line;
            StringBuilder buffer = new StringBuilder();
            while ((line = in.readLine()) != null)
                buffer.append(line).append('\n');

            byte[] latin1 = buffer.toString().getBytes("ISO-8859-1");
            return new String(latin1);

            //return buffer;
        } catch (IOException e) {
            return "";
        } finally {
            closeStream(in);
        }
    }

    private static void closeStream(Closeable stream) {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                // Ignore
            }
        }
    }

}

1 个答案:

答案 0 :(得分:1)

@Pankaj所说的是对的。在进行其他活动之前,您需要关闭进度对话框。