我有服务,它使用 AsyncTask 连接到服务器。解析响应后,我需要发出通知和振动,但得到 NullpointerException。
@Override
protected void onPostExecute(String[] result) {
ReconnectCallback(result);
super.onPostExecute(result);
}
public void ReconnectCallback(String[] params) {
Handler mHandler = new Handler();
Notification notif = new Notification(R.drawable.ic_launcher, "Connection error", System.currentTimeMillis());
Intent x = new Intent();
PendingIntent pIntent = PendingIntent.getActivity(MyApplication.getAppContext() , 0, new Intent(), 0);
notif.flags |= Notification.FLAG_AUTO_CANCEL;
v.vibrate(new long[] { 300, 300 }, -1);
notif.setLatestEventInfo(MyApplication.getAppContext(), "Taxi driver error!", "Err: parse exception", pIntent);
nm.notify(9999, notif);
日志
11-05 12:22:12.829: E/AndroidRuntime(13698): FATAL EXCEPTION: main
11-05 12:22:12.829: E/AndroidRuntime(13698): java.lang.NullPointerException
11-05 12:22:12.829: E/AndroidRuntime(13698): at com.pkg.ReconnectCallback(NetworkService.java:240)
11-05 12:22:12.829: E/AndroidRuntime(13698): at com.pkg.NetworkService$startReconnection.onPostExecute(NetworkService.java:185)
11-05 12:22:12.829: E/AndroidRuntime(13698): at com.pkg.NetworkService$startReconnection.onPostExecute(NetworkService.java:1)
11-05 12:22:12.829: E/AndroidRuntime(13698): at android.os.AsyncTask.finish(AsyncTask.java:417)
11-05 12:22:12.829: E/AndroidRuntime(13698): at android.os.AsyncTask.access$300(AsyncTask.java:127)
11-05 12:22:12.829: E/AndroidRuntime(13698): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
11-05 12:22:12.829: E/AndroidRuntime(13698): at android.os.Handler.dispatchMessage(Handler.java:99)
11-05 12:22:12.829: E/AndroidRuntime(13698): at android.os.Looper.loop(Looper.java:130)
11-05 12:22:12.829: E/AndroidRuntime(13698): at android.app.ActivityThread.main(ActivityThread.java:3835)
11-05 12:22:12.829: E/AndroidRuntime(13698): at java.lang.reflect.Method.invokeNative(Native Method)
11-05 12:22:12.829: E/AndroidRuntime(13698): at java.lang.reflect.Method.invoke(Method.java:507)
11-05 12:22:12.829: E/AndroidRuntime(13698): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
11-05 12:22:12.829: E/AndroidRuntime(13698): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-05 12:22:12.829: E/AndroidRuntime(13698): at dalvik.system.NativeStart.main(Native Method)
v.vibrate(new long[] { 300, 300 }, -1);
nm.notify(9999, notif);
导致 NPE。怎么了?
@Override
public void onCreate() {
// TODO Auto-generated method stub
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
v = (Vibrator) getSystemService(VIBRATOR_SERVICE);
super.onCreate();
}