2

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
Jonas Peteraitis
  • 333
  • 4
  • 16
  • Please show the relevant code and the logcat output for the exception – Simon Sep 23 '12 at 11:54
  • There are quite a few of those here on SO. [One](http://stackoverflow.com/questions/4086159/checking-internet-connection-on-android), [Two](http://stackoverflow.com/questions/4154561/how-to-determine-if-an-internet-connection-is-currently-available-and-active-on), [Three](http://stackoverflow.com/questions/2753412/android-internet-connectivity-check-problem). Search, and you shall receive. Do that before you ask though.. ;-) – Siddharth Lele Sep 23 '12 at 11:57
  • So what's here? Caused by: java.lang.NullPointerException at java.io.StringReader.(StringReader.java:46) – Simon Sep 23 '12 at 12:03

3 Answers3

3

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..

Rahmathullah M
  • 2,676
  • 2
  • 27
  • 44
0

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.

Community
  • 1
  • 1
Vishal Vyas
  • 2,571
  • 1
  • 22
  • 39
0

In the manifest you must add the internet permission :

<uses-permission android:name="android.permission.INTERNET" />
user785975
  • 169
  • 1
  • 3
  • 11