Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6868165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:24:01+00:00 2026-05-27T03:24:01+00:00

I found a code that should check wether the application is offline and has

  • 0

I found a code that should check wether the application is offline and has to load the cache or when in online mode and has to load url. This is the code I used for that:

ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Activity.CONNECTIVITY_SERVICE);
String weblink = "http://www.google.com";
if(cm.getActiveNetworkInfo().isConnected()){
    myWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
    myWebView.loadUrl(weblink);
} else {
    myWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    myWebView.loadUrl(weblink);
}

And these are my permissions:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permisson.ACCESS_WIFI_STATE"/>

My problem: When I’m in airplane mode or my connections are off. The app force closes. When I’m online the app just loads and acts normal… What did I wrong?

This is my logcat:

11-22 10:09:40.310: E/AndroidRuntime(323): FATAL EXCEPTION: main
11-22 10:09:40.310: E/AndroidRuntime(323): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.martijngijselaar.rooster/com.martijngijselaar.rooster.SaxionRoosterActivity}: java.lang.NullPointerException
11-22 10:09:40.310: E/AndroidRuntime(323):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-22 10:09:40.310: E/AndroidRuntime(323):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-22 10:09:40.310: E/AndroidRuntime(323):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-22 10:09:40.310: E/AndroidRuntime(323):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-22 10:09:40.310: E/AndroidRuntime(323):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-22 10:09:40.310: E/AndroidRuntime(323):  at android.os.Looper.loop(Looper.java:123)
11-22 10:09:40.310: E/AndroidRuntime(323):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-22 10:09:40.310: E/AndroidRuntime(323):  at java.lang.reflect.Method.invokeNative(Native Method)
11-22 10:09:40.310: E/AndroidRuntime(323):  at java.lang.reflect.Method.invoke(Method.java:521)
11-22 10:09:40.310: E/AndroidRuntime(323):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-22 10:09:40.310: E/AndroidRuntime(323):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-22 10:09:40.310: E/AndroidRuntime(323):  at dalvik.system.NativeStart.main(Native Method)
11-22 10:09:40.310: E/AndroidRuntime(323): Caused by: java.lang.NullPointerException
11-22 10:09:40.310: E/AndroidRuntime(323):  at com.martijngijselaar.rooster.SaxionRoosterActivity.onCreate(SaxionRoosterActivity.java:34)
11-22 10:09:40.310: E/AndroidRuntime(323):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-22 10:09:40.310: E/AndroidRuntime(323):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-22 10:09:40.310: E/AndroidRuntime(323):  ... 11 more

Okay to make it more easier here is the whole code:

package com.martijngijselaar.rooster;

import android.app.Activity;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class SaxionRoosterActivity extends Activity {
    private static final int MENU1 = Menu.FIRST;
    private static final int MENU2 = Menu.FIRST + 1;
    private static final int MENU3 = Menu.FIRST + 2;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.main);
        WebView myWebView = (WebView) findViewById(R.id.webview);
        myWebView.setWebViewClient(new WebViewClient());

        // Checken of er een connectie is, zo niet dan de chache laden, zo wel dan gewoon het netwerk laden!!
        ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Activity.CONNECTIVITY_SERVICE);
        String weblink = "http://google.com/";
        if(cm.getActiveNetworkInfo().isConnected()){
            myWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
            myWebView.loadUrl(weblink);
        } else {
            myWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
            myWebView.loadUrl(weblink);
        }

        // Zorgen dat je input kunt gebruiken in de webview
        myWebView.requestFocus(View.FOCUS_DOWN);
        myWebView.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_UP:
                        if (!v.hasFocus()) {
                            v.requestFocus();
                        }
                        break;
                }
                return false;
            }

        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, MENU1, 0, "Wijzig klas");
        menu.add(0, MENU2, 0, "Disclaimer");
        menu.add(0, MENU3, 0, "Sluiten");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        WebView myWebView = (WebView) findViewById(R.id.webview);
        switch (item.getItemId()) {
        case MENU1:
            myWebView.loadUrl("http://google.nl");
            return true;
        case MENU2:
            myWebView.loadUrl("http://google.nl");
            return true;
        case MENU3:
            finish();
            return true;
        }
        return false;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:24:02+00:00Added an answer on May 27, 2026 at 3:24 am

    Your check for the connection needs to be tweaked. Although there is a property called isConnected you first need to check if getActiveNetworkInfo() returns null. If it returns null, there is no active network info – this is what you would get in airplane mode.
    This is the check that I use for network connectivity

    private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've found this small code that sends email to gmail users. I'd like the
This morning we found an old chunk of code that was causing a library
A coworker recently showed me some code that he found online. It appears to
While trying to answer this question I found that the code int* p =
I found myself staring at code that looks similar to this: private double? _foo;
I'm using code that I found on the CodeProject.com for a low-level keyboard hook
I recently found myself writing a piece of code that executed a Core Data
I found some code in a project which looks like that : int main(int
Whilst refactoring some legacy C++ code I found that I could potentially remove some
List Comprehension is a very useful code mechanism that is found in several languages,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.