问题
I have service, that connect to server with AsyncTask. After parsing response, i need to make notification and vibrate, but get 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);
Log
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);
causes NPE. What wrong?
@Override
public void onCreate() {
// TODO Auto-generated method stub
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
v = (Vibrator) getSystemService(VIBRATOR_SERVICE);
super.onCreate();
}
回答1:
v
and/or nm
are null
, apparently. Certainly, one of those two is null
, if your stack trace is to be believed.
来源:https://stackoverflow.com/questions/13230162/android-notifications-and-vibrate-from-service