-2

我一直在线收到此错误,但如果我删除空值,我会在上下文中收到错误,告诉我初始化变量..

nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

这是我的代码

            public void createDialog(Enter_med enter_med) {
        // TODO Auto-generated method stub

        Context context = null;

        NotificationManager nm;
          nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);        
          CharSequence from = "VIPUL";
          CharSequence message = "Crazy About Android...";
          PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            new Intent(), 0);
          Notification notif = new Notification(R.drawable.ic_launcher,
            "Crazy About Android...", System.currentTimeMillis());
          notif.setLatestEventInfo(context, from, message, contentIntent);
          nm.notify(1, notif);
    }
4

2 回答 2

1

您应该尝试context在您的onCreate.

Context context;
public void onCreate(...){
   ...
   context = this;
   ...
} 

然后你用它nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

您的问题是您需要使用活动上下文来启动它,而不是null

于 2012-10-02T12:38:02.060 回答
0

您正在使用 null 初始化上下文,然后使用它。
您应该使用活动上下文对其进行初始化

于 2012-10-02T12:28:27.223 回答