1

在我的应用程序中,我有一个服务,它使用处理程序在随机时间后执行可运行文件。该程序运行良好 - 几个小时。突然之间,程序将抛出一个带有以下跟踪的空指针异常:

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

我有另一个程序,它的服务似乎也有这个问题。我只复制了与处理程序和可运行程序有关的代码,并且由两个程序共享。我希望这是足够的信息来解决这个问题。如果需要更多信息,我当然会填写一些空白。这将对这些和我未来的应用程序有很大帮助!

4

1 回答 1

0

我有一个非常相似的问题,因为我只在某些条件下实例化可运行对象,并且也只在某些条件下实际运行 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 个月前提出的,但我认为我的回答可能对其他人有所帮助。:) 希望这可以帮助。

于 2011-07-30T06:10:04.700 回答