在我的应用程序中,我有一个服务,它使用处理程序在随机时间后执行可运行文件。该程序运行良好 - 几个小时。突然之间,程序将抛出一个带有以下跟踪的空指针异常:
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*/
}
我有另一个程序,它的服务似乎也有这个问题。我只复制了与处理程序和可运行程序有关的代码,并且由两个程序共享。我希望这是足够的信息来解决这个问题。如果需要更多信息,我当然会填写一些空白。这将对这些和我未来的应用程序有很大帮助!