简体   繁体   English

java.lang.IllegalArgumentException:需要contentIntent

[英]java.lang.IllegalArgumentException: contentIntent required

The full error also contains: 完整错误还包含:

    android.app.RemoteServiceException: Bad notification for startForeground:

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. 我在这里阅读了其他类似的帖子,尝试了他们的建议并阅读了他们的链接,但是少数用户仍在报告此错误。

Overview 概观

An activity is started by an external application. 活动由外部应用程序启动。 This activity starts a custom speech recognition service. 此活动启动自定义语音识别服务。 It does not use startForeground: 使用startForeground:

    this.startService(intent);

The activity then calls finish(); 然后活动调用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);

The notification has no requirement to do anything 'onClick' as it's cancelled as soon as the user stops talking. 通知没有要求“onClick”做任何事情,因为一旦用户停止说话就会取消。 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. 我最初传递了一个'null intent',然而,在阅读了很多帖子后,我在显示TTS设置的随机意图/ pendingIntent中添加了,只是为了排除这个问题。

99% of my users don't have an issue with either the above code or passing a null intent. 99%的用户没有上述代码或传递空意图的问题。 I need to solve this for the 1% though, as it's a very important part of my application. 我需要为1%解决这个问题,因为它是我应用程序中非常重要的一部分。

Any suggestions would be very much appreciated. 任何建议将非常感谢。

In my case the Logcat debug was eloquent: " android.app.RemoteServiceException: Bad notification for startForeground:" Second line read: PendingIntent null ! 在我的例子中,Logcat调试是雄辩的:“android.app.RemoteServiceException:startForeground的错误通知:”第二行读取: PendingIntent null

In Android 4.x having a null PendingIntent is not a problem for starting foreground services, however in earlier Android versions it is. 在具有空PendingIntent的Android 4.x中,启动前台服务不是问题,但在早期的Android版本中它是。

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. 如果你的代码中存在同样的问题,我会检查pendingIntent是否为null。 This could perhaps happen if mContext is null! 如果mContext为null,则可能会发生这种情况!

Have a look on the documentation for the setLatestEventInfo . 查看setLatestEventInfo的文档。

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 was quoted from Android official documentation . 代码引自Android官方文档

Also see this for a similar issue on StackOverflow. 也看到这个了在计算器上类似的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.IllegalArgumentException:属性'transactionManager'是必需的 - java.lang.IllegalArgumentException: Property 'transactionManager' is required java.lang.IllegalArgumentException - java.lang.IllegalArgumentException java.lang.IllegalArgumentException - java.lang.IllegalArgumentException Roboelectric给了我一个java.lang.IllegalArgumentException:需要INTERNET权限 - Roboelectric is giving me a java.lang.IllegalArgumentException: INTERNET permission is required Spring boot:java.lang.IllegalArgumentException:驱动程序类名称需要jdbcUrl - Spring boot: java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName 致命异常:java.lang.IllegalArgumentException:需要GoogleApiClient参数 - Fatal Exception : java.lang.IllegalArgumentException: GoogleApiClient parameter is required java.lang.IllegalArgumentException:启动tomcat时需要'dataSource'或'jdbcTemplate' - java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required when starting tomcat java.lang.IllegalArgumentException:[断言失败]-此参数是必需的; 它不能为空 - java.lang.IllegalArgumentException: [Assertion failed] - this argument is required; it must not be null java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required - java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required Web视图中的java.lang.IllegalArgumentException - java.lang.IllegalArgumentException in a webview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM