Jonas Peteraitis
Jonas Peteraitis

Reputation: 333

Android force close when no connection

I'm making an aplication that uses internet connection. When i try to download data, but there isn't internet connection application force closes. What do i need to do if I want application not to throw Force close message, but my own message, like "There is no connection..." and then close activity. Thanks!

My logcat output :

09-23 14:56:15.411: E/AndroidRuntime(20770): FATAL EXCEPTION: main
09-23 14:56:15.411: E/AndroidRuntime(20770): java.lang.RuntimeException: Unable to start activity ComponentInfo{lt.prasom/lt.prasom.GoodOffers}: java.lang.NullPointerException
09-23 14:56:15.411: E/AndroidRuntime(20770):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-23 14:56:15.411: E/AndroidRuntime(20770):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-23 14:56:15.411: E/AndroidRuntime(20770):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-23 14:56:15.411: E/AndroidRuntime(20770):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-23 14:56:15.411: E/AndroidRuntime(20770):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-23 14:56:15.411: E/AndroidRuntime(20770):    at android.os.Looper.loop(Looper.java:130)
09-23 14:56:15.411: E/AndroidRuntime(20770):    at android.app.ActivityThread.main(ActivityThread.java:3683)
09-23 14:56:15.411: E/AndroidRuntime(20770):    at java.lang.reflect.Method.invokeNative(Native Method)
09-23 14:56:15.411: E/AndroidRuntime(20770):    at java.lang.reflect.Method.invoke(Method.java:507)
09-23 14:56:15.411: E/AndroidRuntime(20770):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-23 14:56:15.411: E/AndroidRuntime(20770):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-23 14:56:15.411: E/AndroidRuntime(20770):    at dalvik.system.NativeStart.main(Native Method)
09-23 14:56:15.411: E/AndroidRuntime(20770): Caused by: java.lang.NullPointerException
09-23 14:56:15.411: E/AndroidRuntime(20770):    at java.io.StringReader.<init>(StringReader.java:46)
09-23 14:56:15.411: E/AndroidRuntime(20770):    at lt.prasom.functions.XMLParser.getDomElement(XMLParser.java:73)
09-23 14:56:15.411: E/AndroidRuntime(20770):    at lt.prasom.GoodOffers.onCreate(GoodOffers.java:68)
09-23 14:56:15.411: E/AndroidRuntime(20770):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-23 14:56:15.411: E/AndroidRuntime(20770):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-23 14:56:15.411: E/AndroidRuntime(20770):    ... 11 more

Upvotes: 2

Views: 1295

Answers (3)

Rahmathullah M
Rahmathullah M

Reputation: 2716

the below function is used to detect whether device is connected to a network or not.

 public static boolean isNetworkAvailable(Context context) {
     ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
     if (connectivity != null) {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
           for (int i = 0; i < info.length; i++) {
              if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                 return true;
              }
           }
        }
     }
     return false;
  }

You can invoke the function as shown below...

if(!isNetworkAvailable(context))
     // Show Toast here...
else
     //perform action ...

Hope it helps..

Upvotes: 3

Vishal Vyas
Vishal Vyas

Reputation: 2581

Before making network connection, check whether the network connection is available or not. Check this Detect Internet Connection

If the network connection is available, go ahead and do the processing and if not you can use Toast message or an AlertDialog to display appropriate message to the user.

If you are planning to close the activity when the internet connection is not available, display a Toast message and call finish() method. Something like this:

    if (noNetworkAvailable) {
        Toast.makeText(getApplicationContext(), "No network available",
                Toast.LENGTH_SHORT).show();
        finish();
    }

Note: Since the finish() method will close the activity, Use Toast message instead of AlertDialog because it'll cause "android.view.WindowLeaked" exception.

Upvotes: 0

user785975
user785975

Reputation: 169

In the manifest you must add the internet permission :

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

Upvotes: 0

Related Questions