android - java.lang.NoSuchMethodError : android. 应用程序.PendingIntent.getActivity

标签 android android-intent android-notifications

突然间,我开始收到很多崩溃报告,声称 android.app.PendingIntent.getActivity 方法不存在。

有人知道是什么原因造成的吗?

java.lang.NoSuchMethodError: android.app.PendingIntent.getActivity
    at com.example.notification.NotificationHelper.showNotification(NotificationHelper.java:37)
    at com.example.notification.NotificationHelper.showNotification(NotificationHelper.java:19)
    at com.example.content.sync.userdata.TaskSynchronizer$2.onResultReceived(TaskSynchronizer.java:142)
    at com.example.content.sync.BulkRequest.onResultReceived(BulkRequest.java:172)
    at com.example.content.sync.SyncAdapterHelper.pushOrPull(SyncAdapterHelper.java:201)
    at com.example.content.sync.SyncAdapterHelper.syncAll(SyncAdapterHelper.java:60)
    at com.example.content.sync.SyncAdapter.onPerformSync(SyncAdapter.java:139)
    at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:247)

下面是我的 NotificationHelper 类:

public class NotificationHelper {

    public static void showNotification( Context context, String title, String content, MenuItem itemToStart ) {
        showNotification(context, title, content, itemToStart, null, itemToStart.id );
    }
    public static void showNotification( Context context, String title, String content, MenuItem itemToStart, Bundle extras ) {
        showNotification(context, title, content, itemToStart, extras, itemToStart.id );
    }

    public static void showNotification( Context context, String title, String content, MenuItem itemToStart, Bundle extras, int notificationId ) {
        /*
         * Check if user has disabled notifications
         */
        if ( !ProfileManager.areNotificationsEnabled( context ) ) {
            return;
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        Intent contentIntent = new Intent( context, LauncherActivity.class );
        contentIntent.putExtra( MFMainActivity.EXTRA_START_MENUITEM_ID, itemToStart.id );

        builder.setSmallIcon( R.drawable.ic_stat_notify )
               .setAutoCancel( true )
               .setContentTitle( title )
               .setContentText( content )
               .setContentIntent( PendingIntent.getActivity(context, 0, contentIntent, Intent.FLAG_ACTIVITY_NEW_TASK, extras ) );

        NotificationManager nm = (NotificationManager) context.getSystemService( Context.NOTIFICATION_SERVICE );
        nm.notify( notificationId, builder.build() );
    }
}

更新:正如 Tushar 所指出的,我已经开始使用带有 Bundle 参数的 PendingIntent.getActivity() 方法。此方法首先在 API 16 中引入,导致所有使用早期 API 的设备崩溃。

我的解决方案是调用 contentIntent.replaceExtras(extras) 并在内容 Intent 中传递 extras 而不是直接传递给 getActivity() 方法。

最佳答案

getActivity(Context context, int requestCode, Intent intent, int flags, Bundle options)

仅在 Android API 16 (4.1) 中引入。如果您在低于该值的任何对象上运行您的应用程序,它将抛出此异常。

您可能想要使用随 API 1 引入的 getActivity() 版本,它具有签名(注意缺少 Bundle):

getActivity(Context context, int requestCode, Intent intent, int flags)

Source

关于android - java.lang.NoSuchMethodError : android. 应用程序.PendingIntent.getActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15632434/

相关文章:

安卓工作室 : findViewId not working with custom views

android - 如何使用播放/停止按钮显示通知?

android - 如何打开有通知到通知栏的应用?

android - 在 Android 和 iOS 上使用 tf-lite C++ API

java - 数据库访问和电池友好的应用程序

Android Lollipop 5.1 : Fatal signal 11 (SIGSEGV), 代码 2,错误地址 0x9e985ff8 in tid 4093 (RenderThread)/当使用大量 9-patch 图形时

java - 将数据从 Activity 发送到 Android 中的 viewpager fragment

android - 在 Android 上,使用预填充的联系表打开添加新联系人的正确方法是什么?

android - 在 IntentService 中获取地理定位上下文

android - 处理 Android 系统托盘中已关闭的 Firebase 通知