2012-08-17 65 views
2

我正在尝试使用NotificationCompat2 by JakeWharton。 NotificationCompat2的doumentation说NotificationCompat2错误:java.lang.IllegalArgumentException

Simple change your import from android.support.v4.app.NotificationCompat to com.jakewharton.notificationcompat2.NotificationCompat2 and use the NotificatonCompat2.Builder class.

我已经改变了进口com.jakewharton.notificationcompat2.NotificationCompat2和使用下面的代码

Notification notification = new NotificationCompat2.Builder(
     MainActivity.this).setContentTitle("Basic Notification") 
     .setContentText("Basic Notification, used earlier") 
     .setSmallIcon(R.drawable.lock).build(); 
notification.flags |= Notification.FLAG_AUTO_CANCEL; 
NotificationManager notificationManager = (NotificationManager) 
     getSystemService(NOTIFICATION_SERVICE); 
notificationManager.notify(0, notification); 

和应用程序崩溃与以下错误

08-17 20:14:32.400: E/AndroidRuntime(289): FATAL EXCEPTION: main 
08-17 20:14:32.400: E/AndroidRuntime(289): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xyz.notificationchk/com.xyz.notificationchk.MainActivity}: java.lang.IllegalArgumentException: contentIntent required: pkg=com.wissenways.notificationchk id=0 notification=Notification(vibrate=null,sound=null,defaults=0x0) 

我的问题

我是什么导致应用程序崩溃丢失或miscoding?

回答

1

我不知道是什么错误,但这段代码工作

private static final int UPDATE_PROGRESS = 123654789; 
private NotificationManager notifManager; 
private Context mContext; 
private NotificationCompat2.Builder mNotification; 
private String content = ""; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    mContext = MainActivity.this; 
    notifManager = (NotificationManager) 
        mContext.getSystemService(mContext.NOTIFICATION_SERVICE); 
    mNotification = new NotificationCompat2.Builder(mContext) 
      .setSmallIcon(android.R.drawable.sym_def_app_icon) 
      .setTicker("Launch download") 
      .setContentTitle("Downloader") 
      .setContentText(content) 
      .setContentIntent(getPendingIntent()); 
    notifManager.notify(UPDATE_PROGRESS, mNotification.build()); 
} 

private PendingIntent getPendingIntent() { 
    Intent i = new Intent(mContext, MainActivity.class); 
    i.setFlags(FLAG_ACTIVITY_CLEAR_TOP); 
    return PendingIntent.getActivity(mContext, 0, i, 0); 
} 
+0

错误是你缺少'.setContentIntent(getPendingIntent());' - >'java.lang.IllegalArgumentException异常: contentIntent required' – 2013-07-19 02:07:53

相关问题