本文介绍了NotificationCompat2错误:java.lang.IllegalArgumentException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过JakeWharton 使用 NotificationCompat2。 NotificationCompat2的doumentation说:

I am trying to use NotificationCompat2 by JakeWharton. The doumentation of NotificationCompat2 says

简单的改变您的进口从
  android.support.v4.app.NotificationCompat到
  com.jakewharton.notificationcompat2.NotificationCompat2并使用
  NotificatonCompat2.Builder类。

我已经改变了进口 com.jakewharton.notificationcompat2.NotificationCompat2 ,并使用以下code

I have changed the import to com.jakewharton.notificationcompat2.NotificationCompat2 and using the following code

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);

和应用具有以下错误崩溃

and the application crashed with following error

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)

我的提问

我所丢失或错误编码这是造成应用程序崩溃?

What I am missing or miscoding which is causing the application to crash?

推荐答案

我不知道是什么错误,但是这片code作品

I don't know what the error was but this piece of code works

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);
}

这篇关于NotificationCompat2错误:java.lang.IllegalArgumentException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!