处理程序在几个小时后抛出NullPointerException

时间:2010-11-23 06:05:30

标签: android service nullpointerexception handler runnable

在我的应用程序中,我有一个服务,它使用一个处理程序在一段随机的时间后执行一个runnable。该程序运行良好 - 几个小时。突然之间,程序将抛出nullpointerexception并带有以下跟踪:

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)

此外,只要应用程序强制关闭,我就可以重新启动应用程序,并且异常不会再被抛出几个小时。有关处理程序的缩写代码如下:

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*/     

 @Override
 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*/    
}

我有另一个程序似乎也在其服务中遇到此问题。我只复制了与处理程序和runnable有关的代码,并由两个程序共享。我希望这是足够的信息来解决这个问题。如果需要更多信息,我肯定会填补一些空白。对于这些和我未来的应用程序来说,这将是一个很好的帮助!

1 个答案:

答案 0 :(得分:0)

由于我只在某些条件下实例化runnables而且在某些情况下实际上只运行postDelayed,因此我遇到了一个非常类似的问题。 例如

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

为了确保这不会导致力量关闭,我将其更改为

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

现在,如果错误地调用了我的删除回调,则会捕获异常并且不会强制关闭。

可能由于某些条件未得到满足而偶尔没有运行postDelayed,或者在postDelayed启动之前,系统可能已经破坏了服务,导致ondestroy中的removecallbacks被调用。所以,如果你把它放在一个try块中,那么虽然removecallbacks可能仍会失败,但它不会对用户产生影响

我知道这是在8个月前被问到的,但我认为我的回答可能对其他人有帮助。 :) 希望这会有所帮助。