检查互联网连接时Android对话框崩溃

检查互联网连接时Android对话框崩溃,android,crash,toast,dialog,Android,Crash,Toast,Dialog,我有这个MainActivity,但当我尝试在无连接上显示对话框时,应用程序崩溃,但如果我将对话框置于工作连接上,它就会工作。如何解析此应用程序chrash 我也尝试了不同的对话框和吐司,但结果相同 active connetion toast和对话框非常有效 非活动连接和对话框使我的应用程序崩溃 public class MainActivity extends Activity { private WebView mWebView; public static boolea

我有这个MainActivity,但当我尝试在无连接上显示对话框时,应用程序崩溃,但如果我将对话框置于工作连接上,它就会工作。如何解析此应用程序chrash

我也尝试了不同的对话框和吐司,但结果相同

active connetion toast和对话框非常有效

非活动连接和对话框使我的应用程序崩溃

public class MainActivity extends Activity {
    private WebView mWebView;

    public static boolean isNetworkAvailable(Context context) 
    {
        return ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo() != null;
    }

    private void photoline(String text, String link, String desc){

        NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this)

        .setAutoCancel(true)
        .setSmallIcon(R.drawable.photoline)
        .setContentTitle(text)
        .setContentText(desc);


        NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // pending intent is redirection using the deep-link
        Intent resultIntent = new Intent(Intent.ACTION_VIEW);
        resultIntent.setData(Uri.parse(link));

        PendingIntent pending = PendingIntent.getActivity(this, 0, resultIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
        notificationBuilder.setContentIntent(pending);

        // using the same tag and Id causes the new notification to replace an existing one
        mNotificationManager.notify(0, notificationBuilder.build());
    }


    private void oradeatown(String text, String link, String desc){

        NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this)

        .setAutoCancel(true)
        .setSmallIcon(R.drawable.notif)
        .setContentTitle(text)
        .setContentText(desc);


        NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // pending intent is redirection using the deep-link
        Intent resultIntent = new Intent(Intent.ACTION_VIEW);
        resultIntent.setData(Uri.parse(link));

        PendingIntent pending = PendingIntent.getActivity(this, 0, resultIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
        notificationBuilder.setContentIntent(pending);

        // using the same tag and Id causes the new notification to replace an existing one
        mNotificationManager.notify(1, notificationBuilder.build());
    }  

    @Override
    public void onCreate(Bundle savedInstanceState) {




        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        photoline("Photoline Studio","http://www.photoline.ro","App sponsorizata de Photoline Studio");
        oradeatown("Oradea Town (Rate us)","http://play.google.com/store/apps/details?id=com.oradeatown","Da-ne 5 stele :)");


        if (isNetworkAvailable(getBaseContext()))
        {
             mWebView = new WebView(this);
             mWebView.getSettings().setJavaScriptEnabled(true);
             mWebView.loadUrl("http://www.website.com/oradeatown");
        }
        else
        {
            AlertDialog ad = new AlertDialog.Builder(this).create();
            ad.setCancelable(false); // This blocks the 'BACK' button
            ad.setMessage("Hello World");
            ad.setButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();                    
                }
            });
            ad.show();
        } 







        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                 if (url.startsWith("exit:")) { 
                    finish();
                     System.exit(0);

             }

                 if (url.startsWith("res:")) { 
                     Intent i = getBaseContext().getPackageManager()
                             .getLaunchIntentForPackage( getBaseContext().getPackageName() );
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);

              }
                if (url.startsWith("tel:")) { 
                        Intent intent = new Intent(Intent.ACTION_DIAL,
                                Uri.parse(url)); 
                        startActivity(intent); 
                }else if(url.startsWith("http:") || url.startsWith("https:")) {

                    view.loadUrl(url);
                }

                return true;



            }



        });

        this.setContentView(mWebView);
    }





    @Override
    public boolean onKeyDown(final int keyCode, final KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
如果由于语句mWebView=new WebViewthis而导致网络检查失败,则mWebViewObject仍然为空;从来没有人打过电话。您不应该引用那些您不确定它们是否为null的对象。您可以添加如下检查:

if(mWebView !=null){ ... }

您在清单中有权限fpr连接状态吗?添加了mWebView=new WebViewthis;在其他方面,它的工作,谢谢Tmkhva