2013-03-12 56 views
9

在下面的代碼,Eclipse中發現了一個錯誤:問題與NotificationCompact.Builder和ActionBarSherlock

The method build() is undefined for the type NotificationCompat.Builder 

一切加入ActionBarSherlock(以下this tutorial)之前工作的罰款。

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.app.TaskStackBuilder; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.support.v4.app.NotificationCompat; 

public class NotificationActivity extends BroadcastReceiver { 

    NotificationManager nm; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     nm = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     int notifyID = 1; 
     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
       context) 
       .setSmallIcon(R.drawable.zcicon) 
       .setAutoCancel(true) 
       .setDefaults(
         Notification.DEFAULT_SOUND 
           | Notification.DEFAULT_LIGHTS) 
       .setTicker("mytitle").setContentTitle("mycontent") 
       .setContentText("text, text"); 
     Intent resultIntent = new Intent(context, CalcareReader.class); 
     TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
     // Adds the back stack for the Intent (but not the Intent itself) 
     stackBuilder.addParentStack(MyActivity.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); 

     nm.notify(notifyID, mBuilder.build()); // error here 
    } 
} 

回答

21

build()被添加到新版Android支持包中。根據您獲取和設置ActionBarSherlock的方式,您可能正在使用較早版本的Android支持包。確保你已經在SDK Manager中下載了最新版本,然後在ActionBarSherlock項目和你的主應用程序項目中使用這個android-support-v4.jar

+0

工程謝謝! [鏈接](http://stackoverflow.com/questions/12793722/error-on-add-library-project-in-eclipse) – 2013-03-12 13:08:55

+0

>>「ActionBarSherlock項目和你的主要應用程序中的android-support-v4.jar項目。」其實不是。您只需要在ActionBarSherlock項目中使用一個。 – 2013-03-12 13:56:13

+0

@DoctororDrive:True。但是,如果兩個項目中都存在舊的JAR,則只更換一個會導致構建錯誤。兩個項目都需要相同的JAR,或者只在ActionBarSherlock中有JAR,而在主項目中沒有JAR。 – CommonsWare 2013-03-12 16:02:46

0

版本()是從舊版本的Android的支持,v4.jar的

[如果使用的ActionBar福爾摩斯]

1更新從SDK Android的支持庫

2複製粘貼此到您的lib /文件夾,或者更新路徑中的參考號

3對sherlockactionbar項目執行同樣的操作。要小心,如果你有一個Android支持2-v4.jar,將其刪除,只添加了Android系統的支持,v4.jar

4清潔