在我的应用程序中,我正在启动这样的服务:
startService(new Intent(SplashScreen.this, GetContactService.class));
启动服务后,我继续进行另一个活动,然后单击按钮,我已经启动了这样的 Asyctask:
class ABC extends AsyncTask<Void, Void, Void>{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
// my code snippet
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
// my code snippet
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
//stopping the service which I have started earlier
stopService(new Intent(TelaSketchUtil.this, GetContactService.class));
}
}
但是在这里它运行良好,直到在后台运行,但是在 Post Execute 的 stopService 行上,它给出了一个空指针异常:
日志猫:
04-23 15:25:21.705: W/dalvikvm(26710): threadid=1: thread exiting with uncaught exception (group=0x2b547210)
04-23 15:25:21.715: E/AndroidRuntime(26710): FATAL EXCEPTION: main
04-23 15:25:21.715: E/AndroidRuntime(26710): java.lang.NullPointerException
04-23 15:25:21.715: E/AndroidRuntime(26710): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
04-23 15:25:21.715: E/AndroidRuntime(26710): at android.content.ComponentName.<init>(ComponentName.java:75)
04-23 15:25:21.715: E/AndroidRuntime(26710): at android.content.Intent.<init>(Intent.java:3146)
04-23 15:25:21.715: E/AndroidRuntime(26710): at com.telasketch.misc.TelaSketchUtil$GetContactNumber.onPostExecute(TelaSketchUtil.java:225)
04-23 15:25:21.715: E/AndroidRuntime(26710): at com.telasketch.misc.TelaSketchUtil$GetContactNumber.onPostExecute(TelaSketchUtil.java:1)
04-23 15:25:21.715: E/AndroidRuntime(26710): at android.os.AsyncTask.finish(AsyncTask.java:602)
04-23 15:25:21.715: E/AndroidRuntime(26710): at android.os.AsyncTask.access$600(AsyncTask.java:156)
04-23 15:25:21.715: E/AndroidRuntime(26710): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
04-23 15:25:21.715: E/AndroidRuntime(26710): at android.os.Handler.dispatchMessage(Handler.java:99)
04-23 15:25:21.715: E/AndroidRuntime(26710): at android.os.Looper.loop(Looper.java:137)
04-23 15:25:21.715: E/AndroidRuntime(26710): at android.app.ActivityThread.main(ActivityThread.java:4448)
04-23 15:25:21.715: E/AndroidRuntime(26710): at java.lang.reflect.Method.invokeNative(Native Method)
04-23 15:25:21.715: E/AndroidRuntime(26710): at java.lang.reflect.Method.invoke(Method.java:511)
04-23 15:25:21.715: E/AndroidRuntime(26710): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)