1

OK, I have the simplest android studio application.. and it's killing me! I'm not an experience Android or Java developer.

I've searched online and I get older posts about declaring the activity in the manifest, I did that!

I have 3 activities;

  1. Splashscreen, runs great.
  2. WEMProject, runs great.
  3. NoNetworkConnections, "the application has stopped unexpectedly" during runtime.

In my WEMProject I just want to load a Webview if I have a network connection, if not, show a nice "No network present" screen.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (haveNetworkConnection()) {
        setContentView(R.layout.activity_wemproject);
        myWebView = (WebView) findViewById(R.id.WebView);

        myWebView.getSettings().setLoadsImagesAutomatically(true);
        myWebView.getSettings().setJavaScriptEnabled(false);
        myWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        myWebView.loadUrl("https://my.wem.io");
    } else {
        Intent intent = new Intent(WEMProject.this, NoNetworkConnection.class);
        startActivity(intent);
    }
}

This works great, the network check works fine, but if there is no network the:

        Intent intent = new Intent(this, NoNetworkConnection.class);
        startActivity(intent);

part causes the error.. WHY?

The activity is:

public class NoNetworkConnection extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_no_network_connection);
} }

I've declared the activity in my AndroidManifest.XML

    <activity
        android:name=".NoNetworkConnection"
        android:parentActivityName=".WEMProject"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/title_activity_no_network_connection"
        android:theme="@style/FullscreenTheme">
    </activity>

The debug part of the android log:

04-06 14:12:39.668 15630-15630/io.wem.my.mywem E/AndroidRuntime: FATAL EXCEPTION: main
                                                             Process: io.wem.my.mywem, PID: 15630
                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{io.wem.my.mywem/io.wem.my.mywem.WEMProject}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.NetworkInfo.getTypeName()' on a null object reference
                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                 at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                 at android.os.Looper.loop(Looper.java:154)
                                                                 at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                              Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.NetworkInfo.getTypeName()' on a null object reference
                                                                 at io.wem.my.mywem.WEMProject.haveNetworkConnection(WEMProject.java:33)
                                                                 at io.wem.my.mywem.WEMProject.onCreate(WEMProject.java:61)
                                                                 at android.app.Activity.performCreate(Activity.java:6679)
                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                                 at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                 at android.os.Looper.loop(Looper.java:154) 
                                                                 at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

As requested the haveNetworkConnection() method:

private boolean haveNetworkConnection() {
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();

    if (netInfo.getTypeName().equalsIgnoreCase("WIFI"))
        if (netInfo.isConnected())
            haveConnectedWifi = true;
    if (netInfo.getTypeName().equalsIgnoreCase("MOBILE"))
        if (netInfo.isConnected())
            haveConnectedMobile = true;

    return haveConnectedWifi || haveConnectedMobile;
}

What am I missing?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
schilpr
  • 11
  • 2
  • can you show the logs? – muilpp Apr 06 '17 at 14:07
  • _"What am I missing?"_ [The error message from logcat.](http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this#23353174) – Markus Kauppinen Apr 06 '17 at 14:08
  • added to the original post. Thanks for looking at my question! – schilpr Apr 06 '17 at 14:15
  • Like the error log says, in `WEMProject.java` line 33 you are calling `getTypeName()` on a `NetworkInfo` object that has not been initialized. You could add the `haveNetworkConnection()` method to your question, so that somebody can have a look at it and write a proper answer. – Markus Kauppinen Apr 06 '17 at 14:18
  • Can you post the code for the haveNetworkConnection() method – Mohammed Bakr Sikal Apr 06 '17 at 14:20
  • Added the haveNetworkConnection() method. Learned something allready from this thread, didn't know about the log with the debug info ;-) could have found that error myself if I knew where to look. – schilpr Apr 06 '17 at 16:46

1 Answers1

0

Quoting Android's ConnectivityManager documentation on getActiveNetworkInfo():

Returns | a NetworkInfo object for the current default network or null if no default network is currently active

So, it may well return null and your code should handle this case.

For example:

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();

if (netInfo != null) {
    if (netInfo.getTypeName().equalsIgnoreCase("WIFI"))
        if (netInfo.isConnected())
            haveConnectedWifi = true;
        if (netInfo.getTypeName().equalsIgnoreCase("MOBILE"))
            if (netInfo.isConnected())
                haveConnectedMobile = true;
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
  • Thank you, that makes sense as I was testing with the phone in airplane mode, this solved the issue. – schilpr Apr 06 '17 at 17:37