开发者

Handler Throwing NullPointerException After Several Hours

开发者 https://www.devze.com 2023-01-26 05:19 出处:网络
In my application, I have a service that uses a handler to execute a runnable after a randomized amount of time.The program works fine - for a few hours.All of a sudden, the program will throw a nullp

In my application, I have a service that uses a handler to execute a runnable after a randomized amount of time. The program works fine - for a few hours. All of a sudden, the program will throw a nullpointerexception with the following trace:

java.lang.NullPointerException
at com.martin.ontime.OnTimeService$2.run(OnTimeService.java:224)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:144)
at android.app.ActivityThread.main(ActivityThread.java:4937)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)

Additionally, as soon as the app force closes, I can restart the app and the exception won't be thrown for another few hours. Abbreviated code regarding the handler is as follows:

public class AppService extends Service{

 /**XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

 INITIATE ALL THE GLOBAL VARIABLES

 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/

 //Handler that times the processes
 private Handler h = new Handler();

 /**XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/







 /**XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    EXECUTE ALL CREATE FUNCTIONS

 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/

 @Override
 public void onCreate(){

  super.onCreate();

  //Clear all timers
  h.removeCallbacks(setTime);
  h.removeCallbacks(setAuto);

  //Set a timer to start the processes
  h.postDelayed(setAuto, 0);

 }

 /**XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/    







 /**XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    EXECUTE THESE FUNCTIONS WHEN SERVICE IS STOPPED

 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/     

 @Overri开发者_JAVA技巧de
 public void onDestroy(){

  //Clear all timers
  h.removeCallbacks(setTime);
  h.removeCallbacks(setAuto);

 }

    /**XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/     





 /**XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    HANDLER RUNNABLE

 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/     

 //Runnable
 private Runnable setAuto = new Runnable() {

  public void run(){

   //Create a random number generator
   Random r = new Random();

   //Determine a random amount of time until time is changed
   long d = r.nextInt(5) * 60 * 1000;

   //Wait
   h.postDelayed(setTime, d);

  }

 };

 //Runnable used to set a random allowance
 private Runnable setTime = new Runnable() {

  public void run(){

   try {

    //Create a random number generator
    Random r = new Random();

    //Determine a random amount of time until time is changed
    long d = r.nextInt(30) * 60 * 1000 + 1800000;

    //Wait
    h.postDelayed(setAuto, d);

                           ***TRY/CATCH SURROUNDS A DATA OUTPUT STREAM***

   }

   catch (IOException e){

    e.printStackTrace();

   }

  }

 };

    /**XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/    
}

I have another program that also seems to have this problem in its service. I only copied the code that concerns the handler and the runnable and is shared by both programs. I hope this is enough information to sort this problem out. If anymore information is needed, I'll certainly fill in some blanks. It would be a great help for these and my future apps!


I had a very similar issue due to me only instantiating the runnables under certain conditions and also only actually running the postDelayed under certain conditions. For Example

if (conditions){
  myRunnable = new Runnable(){
    @Override
    public void run(){
    }
  }
 myHandler.postDelayed(myRunnable, 1000*60*1);
}
@Override
public void onDestroy(){
myHandler.removeCallbacks
}

To insure this couldn't cause a force close, I changed it to

@Override
public void onDestroy(){
try {
if (conditions)
myHandler.removeCallbacks;
} catch (Exception e){//do nothing
 }

Now, if by mistake my remove callbacks is mistakenly called, the exception is caught and it doesn't force close.

Possibly a postDelayed was occasionally not being run due to certain conditions not being met, or before a postDelayed was started, the system may have destroyed the service, causing the removecallbacks in the ondestroy to be called. So, if you put it in a try block, then while the removecallbacks may still fail, it will not be user impacting

I know this was asked 8 months ago, but I thought my answer might help someone else. :) Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消