onMessageReceived()でペイロードをリッスンした後にアクションを実行しようとしています。実装するアクションのメソッドにProgressBarがあるため、nullポインター例外がスローされます。
以下は私の方法onMessageReceived()
です。
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getNotification() != null) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
public void run() {
roomAvailabilityHelper = new RoomAvailabilityHelper("");
roomAvailabilityHelper.endCurrentMeeting(new RoomAvailabilityActivity());
roomAvailabilityHelper.autoRefreshTimer(new RoomAvailabilityActivity());
Toast.makeText(getApplicationContext(),
"Am working", Toast.LENGTH_SHORT).show();
Log.d("Updated ->","Calendar updated");
}
});
}
}
次に、autoRefreshTimer()
とendCurrentMeeting()
はRoomAvailabilityHelper`クラスで使用できます。以下はその方法です。
public static void endCurrentMeeting(RoomAvailabilityActivity activity) {
activity.progressBar.setVisibility(VISIBLE);
RoomAvailabilityHelper.updateRoomCalendarEvents(activity.mDisposables,
false, activity, 2000);
activity.runOnUiThread(() -> new Handler().postDelayed(() -> {
MeetingRoomDetailFragment meetingRoomDetailFragment = (MeetingRoomDetailFragment)
activity.getSupportFragmentManager()
.findFragmentById(R.id.frame_room_availability_details);
if (meetingRoomDetailFragment != null && activity.sharedPreferences.getString(
CountryActivity.SHARED_ROOM_CHECKED_IN, null) == null) {
meetingRoomDetailFragment.displayCheckInScreen();
}
activity.progressBar.setVisibility(View.GONE);
}, 3000));
}
public static void autoRefreshTimer(RoomAvailabilityActivity activity) {
final int timedPeriod = 300000;
final Handler handler = new Handler();
handler.postDelayed(() -> {
endCurrentMeeting(activity);
autoRefreshTimer(activity);
}, timedPeriod);
}
以下は、発生しているエラーです:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object reference
at com.andela.mrm.room_availability.RoomAvailabilityHelper.endCurrentMeeting(RoomAvailabilityHelper.java:280)
at com.andela.mrm.notifications.Notifications$1.run(Notifications.java:40)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6939)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
だから、接続の方法を探しています活動ではonMessageReceived()
、私はそれを取得することはできません。そのため、アクティビティが原因で、ProgressBarでnullポインタ例外が発生します。
私が提案するのは使用ですLocalBroadcastManager
。FirebaseonMessageReceived()
サービスファイルでイベントを受信するActivity
と、で関数をトリガーし、で関数を呼び出しますActivity
。
あなたの中Activity
でProgressBar
private BroadcastReceiver pushMessageBroadCastReceiver;
//here receive the broadcast from Firebase message,then your thing
//add this inside onCreate()
pushMessageBroadCastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(AppConfig.PUSH_NOTIFICATION)) {
//Here your receive the data from onMessageReceived()
//do your thing here
Log.d("Activity","Is triggerred here")
}
}
};
@Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(pushMessageBroadCastReceiver,
new IntentFilter(AppConfig.PUSH_NOTIFICATION));
}
@Override
protected void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(pushMessageBroadCastReceiver);
}
あなたのAppConfig
クラスで(持っていない場合は作成してください)
// broadcast receiver intent filters
public static final String PUSH_NOTIFICATION = "pushNotification";
あなたにはonMessageReceived()
ここから、下のコードを追加しますが引き金とLocalBroadcastManager
あなたの活動のどれを:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//here you trigger the LocalBroadcastManager in your activity
Intent pushNotification = new Intent(AppConfig.PUSH_NOTIFICATION);
LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加