突然间,我开始收到不少崩溃报告,声称 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 指出的,我已经开始使用PendingIntent.getActivity()
带有Bundle
参数的方法。这种方法最初是在 API 16 中引入的,它导致所有具有早期 API 的设备上的崩溃。
我的解决方案是在内容 Intent 中调用contentIntent.replaceExtras( extras )
并传递额外内容,而不是直接传递给getActivity()
方法。