NullPointerException when receiving additional from intent on the receiver. Only on Huawei devices

I found on my bugtracker strange NullPointerExceptionin my application. The interesting part is that it is found only on Huawei devices (Honor 7 and P8 Lite).

So, I checked a little test code to see if this is really a problem with these devices.

Here is my code to run AlarmManager:

final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    final Intent intent = new Intent(this, TestReceiver.class);
    intent.putExtra(Constants.contant1, new User("John"));
    intent.setAction(Constants.action1);

    final PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 4882, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager.cancel(pendingIntent);
    if (Build.VERSION.SDK_INT < 19) {
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pendingIntent);
    } else {
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pendingIntent);
    }
}

So, I run AlarmManagerfor a call TestReceiverthat looks like this:

public class TestReceiver extends BroadcastReceiver {

private static final String TAG = TestReceiver.class.getName();

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Constants.action1)) {
            User text = intent.getParcelableExtra(Constants.contant1);
            Log.d(TAG, text.getName());
        }
    }
}

, , . . Huawei Honor 7 NullPointerException, ( Nexus 5, Samsung Galaxy S3, HTC One, LG G2 Mini). , Serializable Parcelable. . Huawei, String extra .

- , ?

+4
1

if(intent.hasExtra(Constants.contant1)){

    User text = intent.getParcelableExtra(Constants.contant1);
    Log.d(TAG, text.getName());
}
0

Source: https://habr.com/ru/post/1619493/


All Articles