私のアプリには地元の人々のリストが含まれており、各地方には日付があります。ローカルをクリックすると、ローカル情報で新しいアクティビティが開き、この日付に基づいてアラームが作成されます。警報は、この地方で提供された日付の2日前に起きるべきです。これは私がそれをやっている方法です:BroadCastReceiverクラスのNullPointerException。
私は地元のアクティビティを入力した場合:
private void scheduleAlarm(Date notificationDate) {
Intent intent = new Intent(getApplicationContext(), Receiver.class).putExtra("myString", myString);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
alarm.set(AlarmManager.RTC, notificationDate.getTime(), pendingIntent);
}
レシーバクラス:マニフェストに登録
public class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String myString = intent.getStringExtra("myString");
if (myString.matches("myString") Toast.makeText(context, "Working", Toast.LENGTH_SHORT).show();
}
}
:
<receiver android:name=".Receiver" android:enabled="true" >
<intent-filter android:priority="999" >
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
これは正常に動作しますが、電話機を再起動すると、メイン画面に入るとすぐにアプリがクラッシュします。
私のアプリにfabric crashlyticsを実装しました。クラッシュをチェックすると、Receiver.classでNullPointerException:pattern == nullが発生します。
私の賭けは、私は自分の携帯電話を再起動したときに、私は意思のエキストラを失うと、この行は、NullPointerExceptionが取得されていることである。
String myString = intent.getStringExtra("myString");
は、誰もが任意の手掛かりを持っていますか?このクラッシュをどうやって解決するべきですか?
myStringがヌルかどうかを確認して ""と一致し、それ以外の場合のみ実行するとクラッシュしないので、電話を再起動するとエキストラがnullになるため、このクラッシュが実際に発生します。 – Ravers