Question

I have this MainActivity but when i try to show a dialog box on no connetion the app crashes but if i put the dialog box on working connection it works . how can i resolve this app chrash ?

I tried differnt dialog boxes and toast too but same result

active connetion toast and dialog box works perfectly

inactive connection toast and dialog box crashes my app

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);
    }
}

if

Was it helpful?

Solution

The mWebViewObject is still null if the network check fails because the statement mWebView = new WebView(this); is never called. You shouldn't make references to objects of which you are not sure they are not null. You could add a check like:

if(mWebView !=null){ ... }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top