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;
- Splashscreen, runs great.
- WEMProject, runs great.
- 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?