Android通知和服务振动 [英] Android notifications and vibrate from service

查看:67
本文介绍了Android通知和服务振动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有服务,该服务使用AsyncTask连接到服务器.解析响应后,我需要进行通知和振动,但会收到NullpointerException.

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);

日志

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();
}

推荐答案

v 和/或 nm 显然是 null .当然,如果您相信堆栈跟踪,那么这两个之一就是 null .

v and/or nm are null, apparently. Certainly, one of those two is null, if your stack trace is to be believed.

这篇关于Android通知和服务振动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆