JesperB
JesperB

Reputation: 4777

java.lang.NoSuchMethodError: android.app.PendingIntent.getActivity

All of the sudden, I started getting quite a few crash reports claiming that the android.app.PendingIntent.getActivity method doesn't exist.

Anyone knows what could be causing this?

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)

Below is my NotificationHelper class:

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() );
    }
}

UPDATE: As Tushar pointed out, I had started using the PendingIntent.getActivity() method with the Bundle argument. This method had first been introduced in API 16, which caused the crashes on all devices with earlier API's.

My solution was to call contentIntent.replaceExtras( extras ) and pass the extras in the content Intent instead of directly to the getActivity() method.

Upvotes: 3

Views: 1295

Answers (1)

Tushar
Tushar

Reputation: 8049

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

was only introduced in Android API 16 (4.1). If you run your app on anything below that, it will throw this exception.

You might want to use the version of getActivity() that was introduced with API 1, which has the signature (notice lack of Bundle):

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

Source

Upvotes: 5

Related Questions