java.lang.IllegalArgumentException异常:要求contentIntent [英] java.lang.IllegalArgumentException: contentIntent required

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

问题描述

完整的错误还包括:

    android.app.RemoteServiceException: Bad notification for startForeground:

我读过其他类似的帖子<一个href=\"http://stackoverflow.com/questions/3112008/android-java-lang-illegalargumentexception-contentintent-required-error-cause\">here,试过他们的意见和阅读他们的链接,但一小部分用户数量还在报告这个错误。

I've read other similar posts here, tried their suggestions and read their links, but a small number of users are still reporting this error.

概述

一个活动是由一个外部应用程序启动。该活动启动自定义语音识别服务。它的使用startForeground:

An activity is started by an external application. This activity starts a custom speech recognition service. It does not use startForeground:

    this.startService(intent);

活动,然后调用完成();

The activity then calls finish();

在服务启动自定义语音识别类,并在构造函数中传递的上下文给它。对我显示了以下通知检测到语音的开始:

The service starts the custom speech recognition class and passes context to it in a constructor. On 'beginning of speech detected' I display the following notification:

    String notTitle = "Hello";
    String notificationText = "hello there";

    notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    myNotification = new Notification(
                android.R.drawable.ic_btn_speak_now, notTitle,
                System.currentTimeMillis());
    myNotification.flags |= Notification.FLAG_AUTO_CANCEL;

    Intent intent = new Intent();
    intent.setAction("com.android.settings.TTS_SETTINGS");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
        myNotification.contentIntent = pendingIntent;

    myNotification.setLatestEventInfo(mContext, notTitle,
                notificationText, pendingIntent);

    notificationManager.notify(MY_NOTIFICATION_ID, myNotification);

通知并没有要求做任何事情的onClick,因为它是只要用户停止说话取消。原本我是通过一个空的意图,但是,读书很多帖子以后,我的意图随意添加/ TTS显示设置中的PendingIntent,只是为了排除这一点的问题。

The notification has no requirement to do anything 'onClick' as it's cancelled as soon as the user stops talking. I was originally passing a 'null intent', however, after reading many posts, I added in the random intent/pendingIntent of displaying TTS Settings, just to rule this out as the problem.

我的99%的用户没有与上述任何code或传递一个空的意图的问题。我需要,虽然解决了这个为1%,因为它是我的应用程序中非常重要的一部分。

99% of my users don't have an issue with either the above code or passing a null intent. I need to solve this for the 1% though, as it's a very important part of my application.

任何建议将是非常美联社preciated。

Any suggestions would be very much appreciated.

推荐答案

在我的情况下logcat的调试有口才:android.app.RemoteServiceException:为startForeground坏的通知:
下联阅读:的PendingIntent空

In my case the Logcat debug was eloquent: " android.app.RemoteServiceException: Bad notification for startForeground:" Second line read: PendingIntent null!

在有一个空的PendingIntent不启动前台服务问题的Andr​​oid 4.x的,但是在早期的Andr​​oid版本中,它是。

In Android 4.x having a null PendingIntent is not a problem for starting foreground services, however in earlier Android versions it is.

所以首先仔细检查调试日志(读取所有的行,为什么不,张贴在这里)。
如果它在你的code同样的问题,我会检查的PendingIntent不为空。这或许可以发生,如果 mContext 为空!

So first of all check the debug log carefully (read all the lines and why not, post them here). If it is the same problem in your code I would check that the pendingIntent is not null. This could perhaps happen if mContext is null!

对了的 <一的文档的外观href=\"http://developer.android.com/reference/android/app/Notification.html#setLatestEventInfo%28android.content.Context,%20java.lang.CharSequence,%20java.lang.CharSequence,%20android.app.PendingIntent%29\"相对=nofollow> setLatestEventInfo

下面是你的code应该如何看(如果你是从一个服务推动这些通知):

Here is how your code should look (if you are pushing these notifications from a service):

        // The PendingIntent to launch our activity if the user selects this notification
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, LocalServiceActivities.Controller.class), 0);

    // Set the info for the views that show in the notification panel.
    notification.setLatestEventInfo(this, getText(R.string.local_service_label),
                   text, contentIntent);

code从的Andr​​oid官方文档引用。

另请参阅<一个href=\"http://stackoverflow.com/questions/11947928/startforeground-bad-notification-error/12138741#12138741\">this对于在计算器上类似的问题。

Also see this for a similar issue on StackOverflow.

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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆