java - 异常 : java. lang.RuntimeException 在 android.app.ActivityThread.handleReceiver

标签 java android notifications media-player

我在 Google Play 中发布了一个应用,但我在 Google Console 中遇到了以下崩溃报告,任何人都可以帮助我了解此问题的根源以及如何解决。

java.lang.RuntimeException: at android.app.ActivityThread.handleReceiver (ActivityThread.java:3523) at android.app.ActivityThread.access$1400 (ActivityThread.java:207) at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1759) at android.os.Handler.dispatchMessage (Handler.java:106) at android.os.Looper.loop (Looper.java:193) at android.app.ActivityThread.main (ActivityThread.java:6863) at java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:537) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:858) Caused by: java.lang.IllegalStateException: at android.support.v4.media.session.MediaButtonReceiver.onReceive (MediaButtonReceiver.java:124) at android.app.ActivityThread.handleReceiver (ActivityThread.java:3507)

通知:

void startNotify(Context context, String playbackStatus, String title) {
        String titlesonge;
        String artist;
        try {
            titlesonge = StringUtils.substringBefore(title, " - ");
            artist = StringUtils.substringAfter(title, " - ");
        } catch (Exception e) {
            titlesonge = title.substring(0, title.indexOf(" - "));
            artist = title.substring(title.lastIndexOf(" - ") - 1);
        }
        int icon = R.drawable.ic_pause_white;
        Intent playbackAction = new Intent(service, RadioService.class);
        playbackAction.setAction(RadioService.ACTION_PAUSE);
        PendingIntent action = PendingIntent.getService(service, 1, playbackAction, 0);
        if (playbackStatus.equals(PlaybackStatus.PAUSED)) {
            icon = R.drawable.ic_play_white;
            playbackAction.setAction(RadioService.ACTION_PLAY);
            action = PendingIntent.getService(service, 2, playbackAction, 0);

        }
        Intent stopIntent = new Intent(service, RadioService.class);
        stopIntent.setAction(RadioService.ACTION_STOP);
        PendingIntent stopAction = PendingIntent.getService(service, 3, stopIntent, 0);

        Intent intent = new Intent(service, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP |
                Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(service, 0, intent, 0);
        notificationManager.cancel(NOTIFICATION_ID);
        String PRIMARY_CHANNEL = "PRIMARY_CHANNEL_ID";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager manager = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE);
            String PRIMARY_CHANNEL_NAME = "PRIMARY";
            NotificationChannel channel = new NotificationChannel(PRIMARY_CHANNEL, PRIMARY_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
            channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
            if (manager != null) {
                manager.createNotificationChannel(channel);
            }
        }
        NotificationCompat.Builder builder = new NotificationCompat.Builder(service, PRIMARY_CHANNEL)
                .setAutoCancel(false)
                .setContentTitle(titlesonge)
                .setContentText(artist)
                .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.largeicon))
                .setContentIntent(pendingIntent)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setSmallIcon(R.drawable.smallwidth)
                .setColor(ContextCompat.getColor(context, R.color.colorneeded))
                .addAction(icon, "pause", action)
                .addAction(R.drawable.ic_stop_white, "stop", stopAction)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setWhen(System.currentTimeMillis())
                .setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle()
                        .setMediaSession(service.getMediaSession().getSessionToken())
                        .setShowActionsInCompactView(0, 1)
                        .setShowCancelButton(true)
                        .setCancelButtonIntent(stopAction));
        service.startForeground(NOTIFICATION_ID, builder.build());

    }

最佳答案

这将分三步解决。 1:_ 打开你的 build.gradle(Module: app) 文件。 将其添加到应用程序标签下

defaultConfig {
multiDexEnabled true
}

2:_ 如果您使用 appcompact,请在依赖项标签下添加此行

dependencies {
implementation 'com.android.support:multidex:1.0.3'
}

如果您将项目迁移到 AndroidX,则添加此行

dependencies {
implementation 'androidx.multidex:multidex:2.0.1'
}

3:_ 打开所有 Activity java 文件并添加 MultiDex.intall(contextName)

protected void onCreate(Bundle savedInstanceState) {
MultiDex.install(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mine);
        `enter code here`
}

关于java - 异常 : java. lang.RuntimeException 在 android.app.ActivityThread.handleReceiver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56121902/

相关文章:

Android/任务 checkDebugAarMetadata 执行失败/执行 CheckAarMetadataWorkAction 时发生故障

Android:应用程序启动的回调

notifications - 锁屏时 VoIP 推送通知在 iOS 10 上不起作用

java - Android -- Google+ 即时上传如何工作?

java - 设置在特定时间通知android

java - Fedora 的 OpenJDK 发行版的许可和更新

java - JPanel 出现在两个位置

java - 使用ANT自动切换JDK/JRE版本

c# - 防止 UI 在处理数据时卡住

android - Android 中 SQLiteDatabase.CursorFactory 的用途是什么?