2013-06-19 44 views
3

當我試圖把我的通知代碼到按鈕 它總是給我的錯誤在這部分的Android NotificationCompat.Builder是不確定

NotificationCompat.Builder mBuilder = 新NotificationCompat.Builder(本)

[[構造函數NotificationCompat.Builder(new View.OnClickListener(){})未定義]]

我該如何解決這個問題???

Button button9= (Button) findViewById(R.id.button9); 
    button9.setOnClickListener(new View.OnClickListener(){ 
    public void onClick(View arg0) { 


     ///////////My Notification//////////////////////////   
     NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(this) 
     .setSmallIcon(R.drawable.notification_icon) 
     .setContentTitle("I'm astm loooooooool") 
     .setContentText("Hello baby to my world!"); 
     // Creates an explicit intent for an Activity in your app 
     Intent resultIntent = new Intent(this, MainActivity.class); 

     // The stack builder object will contain an artificial back stack for the 
     // started Activity. 
     // This ensures that navigating backward from the Activity leads out of 
     // your application to the Home screen. 
     TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
     // Adds the back stack for the Intent (but not the Intent itself) (ResultActivity) 
     stackBuilder.addParentStack(MainActivity.class); 
     // Adds the Intent that starts the Activity to the top of the stack 
     stackBuilder.addNextIntent(resultIntent); 
     PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT); 
     mBuilder.setContentIntent(resultPendingIntent); 
     NotificationManager mNotificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     int mId = 0; 
     // mId allows you to update the notification later on. 
     mNotificationManager.notify(mId, mBuilder.build()); 
     ///////////End Notification////////////////////////// 
     } 
    }); 

回答

2

謝謝你們對我的幫助[沒有人給我任何答案looooooool]

我與我的自我 答案只是定義NotificationCompat.Builder爲最終和它運作良好

^__^

 ///////// my Nine button (set Notification) ////////// 
    final NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(this) 
    .setSmallIcon(R.drawable.notification_icon) 
    .setContentTitle("I'm astm loooooooool") 
    .setContentText("Hello baby to my world!"); 
    Button button9= (Button) findViewById(R.id.button9); 
    final TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
    button9.setOnClickListener(new View.OnClickListener(){ 
    public void onClick(View arg0) { 

     ///////////My Notification//////////////////////////   
     // Creates an explicit intent for an Activity in your app 
     Intent resultIntent = new Intent(); 

     // The stack builder object will contain an artificial back stack for the 
     // started Activity. 
     // This ensures that navigating backward from the Activity leads out of 
     // your application to the Home screen. 
     // Adds the back stack for the Intent (but not the Intent itself) (ResultActivity) 
     stackBuilder.addParentStack(MainActivity.class); 
     // Adds the Intent that starts the Activity to the top of the stack 
     stackBuilder.addNextIntent(resultIntent); 
     PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT); 
     mBuilder.setContentIntent(resultPendingIntent); 
     NotificationManager mNotificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     int mId = 0; 
     // mId allows you to update the notification later on. 
     mNotificationManager.notify(mId, mBuilder.build()); 
     ///////////End Notification////////////////////////// 

     } 
    }); 
0

我有同樣的問題。我改變:

NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(this) 

有:

NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(this.context) 
1

變化thisgetBaseContext

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getBaseContext()); 
相關問題