2015-11-02 22 views
-1

我試圖把PendingIntent通知放在我的android項目,但我有這個錯誤,它說錯誤的第一個參數發現android view.View.OnClickListener需要android內容,上下文。安卓工作室錯誤放在通知函數使用if else

我打算顯示代碼錯誤,「其他通知」下的「this」有錯誤。

PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0); 
         Notification noti = new Notification.Builder(this) 

此錯誤

button.setOnClickListener(new View.OnClickListener() { 


    @Override 
    public void onClick(View view) { 

     int targetId = view.getId(); 


     if (targetId == R.id.button) { 


      Toast.makeText(getApplicationContext(), "You have " + numMessages++ + " message", Toast.LENGTH_SHORT).show(); 


     } else { 
      if (numMessages > 0) { 
       Intent intent = new Intent(); 
       PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0); 
       Notification noti = new Notification.Builder(this) 
         .setTicker("You Have " + numMessages++ + " message") 
         .setContentTitle("OneDiver") 
         .setContentText("Content") 
         .setSmallIcon(R.drawable.ic_od_icon_24dp) 
         .setContentIntent(pIntent).getNotification(); 
       noti.flags = Notification.FLAG_AUTO_CANCEL; 
       NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
       notificationManager.notify(0, noti); 


      } 
     } 
+0

像'YourActivityName.this'這樣寫'YourActivityName.this'代替'this' –

+0

你有關於如何在應用程序中放置通知的喜好,比如在facebook上彈出一個圖標。我已經搜索了這個引用,但沒有結果回答我的問題,關於如何將通知放在應用程序上。謝謝! – jaylord

回答

2

如果這個代碼是一個活動,說YourActivity.class,則需要更換 「本」 與 「YourActivity.this」。因爲在OnClickListener中,「this」引用的是OnClickListener而不是活動。

+0

你有喜歡關於如何在應用程序中放置通知的喜好,比如在Facebook上彈出一個圖標。我已經搜索了這個引用,但沒有結果回答我的問題,關於如何將通知放在應用程序上。謝謝! – jaylord