我正在尝试在监听onMessageReceived()中的有效负载之后执行Action,我想实现的action在其方法中具有一个ProgressBar,因此它抛出了一个空指针异常。
下面是我的方法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()
中连接活动的方法,但我不明白。这就是为什么由于Activity导致ProgressBar上出现空指针异常的原因, 最佳答案
我建议使用LocalBroadcastManager
。因此,当您在Firebase服务文件的onMessageReceived()
中收到事件时,它将触发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);
}