2012-10-02 35 views
-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); 
    } 
+0

的錯誤意味着上下文是空 – Blackbelt

+2

和'上下文語境= null;'有種解釋它 – keyser

+0

你定義上下文爲空行兩上你得到錯誤。 – Zyber

回答

1

您應該嘗試在onCreate中啓動context

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

然後你nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

您的問題,使用它是你需要與活動範圍內啓動它,而不是null

+1

您將以這種方式泄漏活動。正確的解決方案是'context = getApplicationContext();'。 – Alex

0

要初始化背景與空,然後使用它..
你應該與活動場景initialaize它