Вопрос

I am new to android, as my study project I made an application. Which is syncing data from webservice and fetching in my application.

Everything working fine.

But in this scenario

  1. Launch the application
  2. Check for internet connection
  3. Get content from server
  4. Fetch data in application

5. When we turn off Mobile Data / WiFi , then press any button in application suddenly application crashes.

There is any way to handle this issue ?

Thanks in advance

Error message

08-14 21:28:57.670: E/AndroidRuntime(7466): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.tvc/com.myapp.tvc.SingleActivity}: java.lang.NullPointerException
08-14 21:28:57.670: E/AndroidRuntime(7466):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
08-14 21:28:57.670: E/AndroidRuntime(7466):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
08-14 21:28:57.670: E/AndroidRuntime(7466):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
08-14 21:28:57.670: E/AndroidRuntime(7466):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
08-14 21:28:57.670: E/AndroidRuntime(7466):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-14 21:28:57.670: E/AndroidRuntime(7466):     at android.os.Looper.loop(Looper.java:137)
08-14 21:28:57.670: E/AndroidRuntime(7466):     at android.app.ActivityThread.main(ActivityThread.java:4895)
08-14 21:28:57.670: E/AndroidRuntime(7466):     at java.lang.reflect.Method.invokeNative(Native Method)
08-14 21:28:57.670: E/AndroidRuntime(7466):     at java.lang.reflect.Method.invoke(Method.java:511)
08-14 21:28:57.670: E/AndroidRuntime(7466):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
08-14 21:28:57.670: E/AndroidRuntime(7466):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
08-14 21:28:57.670: E/AndroidRuntime(7466):     at dalvik.system.NativeStart.main(Native Method)
08-14 21:28:57.670: E/AndroidRuntime(7466): Caused by: java.lang.NullPointerException
08-14 21:28:57.670: E/AndroidRuntime(7466):     at com.myapp.tvc.SingleActivity.onCreate(SingleActivity.java:76)
08-14 21:28:57.670: E/AndroidRuntime(7466):     at android.app.Activity.performCreate(Activity.java:5163)
08-14 21:28:57.670: E/AndroidRuntime(7466):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
08-14 21:28:57.670: E/AndroidRuntime(7466):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
08-14 21:28:57.670: E/AndroidRuntime(7466):     ... 11 more
Это было полезно?

Решение

whatever is at line 76 of SingleActivity is null, since you did not provide code the best we can say is find out what and why is null at that line

Другие советы

My most common error is something is nulll. Maybe the handler is defined inline and your using a variable that is null

Based on what you're describing and without seeing your code it sounds like you're just trying to connect to the internet when no internet connection exists. To fix this problem you can create a network checker/listener that can check network availability before you try to do anything that requires an internet connection. If no connection is available you just don't run the task that gets stuff from the server. A more involved set up could be designed using a BroadcastReceiver (e.g. so you know when connectivity becomes available again) but try this for now and see if that's your issue.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top