服务、计时器和通知..但是有一个错误.:)



我正在创建我的第一个服务。我有一个带计时器的服务,当计时器用完时,如果条件得到验证,我想发送通知。这是代码:

package app.tdgpisa.service;
import java.util.Timer;
import java.util.TimerTask;
import org.json.JSONArray;
import org.json.JSONException;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import app.tdgpisa.Bacheca;
import app.tdgpisa.R;
import app.tdgpisa.db.Database;
public class NotificaBacheca extends Service {
    private Timer miotimer = new Timer();
    private static  long intervallo = 10*1000;
    private long msg =0;
    Database database=new Database();
    private static final int SIMPLE_NOTIFICATION_ID = 1;
    NotificationManager mNotificationManager;
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onCreate() {
        Log.d("notifica", " inizio create");
        String messaggi = database.MessaggiNum();
        try {
            JSONArray jsonArray = new JSONArray(messaggi);
            for (int i = 0; i < jsonArray.length(); i++)
            {
                msg = jsonArray.getJSONObject(i).getInt("NUM");
                Log.d("notifica", "numero msg nel create: "+ msg);
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    @Override
    public void onDestroy() {
    }
    @Override
    public void onStart(Intent intent, int startid) {
        StartServUpdateTask();
    }    
    private void StartServUpdateTask() {
        miotimer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                Log.d("notifica", "parte il run");
                String messaggi = database.MessaggiNum();
                JSONArray jsonArray;
                try {
                    jsonArray = new JSONArray(messaggi);
                    long newmsg = jsonArray.getJSONObject(0).getLong("NUM");
                    Log.d("notifica", "numero msg nel run: "+ newmsg);
                    if (newmsg>msg){
                        msg=newmsg;
                        Log.d("notifica", "sono diversi, parte la notifica");
                        Notifica();
                    }
                } catch (JSONException e) {
                }
            }
        }, 0, intervallo);
    }
    private void Notifica() {
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(NotificaBacheca.this);
        notificationBuilder.setContentTitle("TITOLO");
        notificationBuilder.setContentText("TESTO");
        notificationBuilder.setTicker("SCROLL TESTO");
        notificationBuilder.setWhen(System.currentTimeMillis());
        notificationBuilder.setSmallIcon(R.drawable.ic_launcher);
        notificationBuilder.setAutoCancel(true);
        Intent notificationIntent = new Intent(NotificaBacheca.this, Bacheca.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notificationBuilder.setContentIntent(contentIntent);
        notificationBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
        mNotificationManager.notify(SIMPLE_NOTIFICATION_ID, notificationBuilder.build());
    }

}

这就是错误:

11-19 13:51:56.752: E/AndroidRuntime(343): FATAL EXCEPTION: Timer-0
11-19 13:51:56.752: E/AndroidRuntime(343): java.lang.NullPointerException
11-19 13:51:56.752: E/AndroidRuntime(343):  at app.tdgpisa.service.NotificaBacheca.Notifica(NotificaBacheca.java:104)
11-19 13:51:56.752: E/AndroidRuntime(343):  at app.tdgpisa.service.NotificaBacheca.access$2(NotificaBacheca.java:87)
11-19 13:51:56.752: E/AndroidRuntime(343):  at app.tdgpisa.service.NotificaBacheca$1.run(NotificaBacheca.java:78)
11-19 13:51:56.752: E/AndroidRuntime(343):  at java.util.Timer$TimerImpl.run(Timer.java:284)

有人能帮我吗?很抱歉,最终,犯了一个大错误…:)非常感谢。

mNotificationManager为null。它从未被初始化为任何东西。空指针行号应该已经告诉了你所需要的一切。

您的mNotificationManager从未设置,但您在第104行调用mNotificationManager.notify(SIMPLE_NOTIFICATION_ID, notificationBuilder.build());

您应该在之前设置mNotificationManager。

相关内容

最新更新