Android DialogBox崩溃检查互联网连接

时间:2014-04-04 22:11:53

标签: android crash toast dialog

我有这个MainActivity但是当我尝试在没有连接的情况下显示一个对话框时,应用程序崩溃,但如果我将对话框放在工作连接上,它就可以工作了。我怎么能解决这个应用程序chrash?

我尝试了不同的对话框和烤面包,但结果相同

主动连接吐司和对话框完美运行

非活动连接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);
    }
}

如果

1 个答案:

答案 0 :(得分:0)

如果网络检查失败,则mWebView对象仍为空,因为永远不会调用语句mWebView = new WebView(this);。您不应该引用您不确定它们不为空的对象。您可以添加一个支票,如:

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