我有一个来自/发送到片段的通知问题。我将尽可能清楚地解释我的情况,以表明问题。
我有一个活动,mywork .java,这个活动有一个可扩展的列表视图。可展开列表视图中的所有项都将用户发送到第二个活动WorkoutDaysActivity。它们都发送了一个单独的额外意图,所以我知道用户点击了哪个项目。在WorkoutDaysActivity.java中,创建了3个片段,星期一、星期三和星期五。每项活动我都有单独的班级。这些类有一个回收器视图和一个计时器。当计时器结束时,用户会得到一个通知。这是有效的,但是当我尝试将用户从通知发送到WorkoutDaysActivity时,它崩溃并出现以下错误。
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
at com.bestworkouts.sheikoworkout.WorkoutDaysActivity.addPages(WorkoutDaysActivity.java:45)
at com.bestworkouts.sheikoworkout.WorkoutDaysActivity.onCreate(WorkoutDaysActivity.java:29)
它发送给我的那行也是它试图根据mywork .java传递的意图设置标题(下面的开关语句)的地方。我猜这是因为意图不是传递,因为他们得到这个类从通知,而不是从mywork .java,因此字符串标题为空。我的问题是我不知道如何解决这个问题。如果我将通知发送到mywork .java,则通知工作正常,但如果可能的话,我更愿意将其发送回计时器设置的地方。
public class WorkoutDaysActivity extends BaseActivity{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.workout_days);
mToolBar = activateToolbar();
setUpNavigationDrawer();
ViewPager vp = (ViewPager) findViewById(R.id.view_pager);
this.addPages(vp);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(vp);
tabLayout.setOnTabSelectedListener(listener(vp));
}
//ADD ALL PAGES
private void addPages(ViewPager pager) {
MyFragPagerAdapter adapter = new MyFragPagerAdapter(getSupportFragmentManager());
Intent intent = getIntent();
String workout = intent.getStringExtra("workout");
Bundle args = new Bundle();
args.putString("workout", workout);
String title = args.getString("workout");
switch(title) {
case "w29w1" : {
getSupportActionBar().setTitle("Workout 29 Week 1");
break;
}
case "w29w2" : {
getSupportActionBar().setTitle("Workout 29 Week 2");
break;
}
MondayFragment mondayFragment = new MondayFragment();
mondayFragment.setArguments(args);
adapter.addPage(mondayFragment);
WedFragment wedFragment = new WedFragment();
wedFragment.setArguments(args);
adapter.addPage(wedFragment);
FridayFragment fridayFragment = new FridayFragment();
fridayFragment.setArguments(args);
adapter.addPage(fridayFragment);
pager.setAdapter(adapter);
}
这是定时器的位置(MondayFragment.java):
private Button btnStart;
private Button btnStop;
private Button btnReset;
private CountDownTimer mCountDownTimer;
private TextView txtViewRest;
private void start() {
mCountDownTimer = new CountDownTimer(5000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
txtViewRest.setText("Rest Remaining: " + millisUntilFinished / 1000 + " seconds.");
}
@Override
public void onFinish() {
txtViewRest.setText("Done!");
showNotification();
}
}.start();
}
public void cancel() {
if (mCountDownTimer != null) {
mCountDownTimer.cancel();
txtViewRest.setText("Rest Remaining: 5 seconds");
} else return;
}
public void reset() {
if (mCountDownTimer != null) {
mCountDownTimer.cancel();
txtViewRest.setText("Rest Remaining: 5 Seconds");
} else return;
}
public void showNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext());
builder.setSmallIcon(R.drawable.sheikoicon);
builder.setContentTitle("Sheiko Rest Timer");
builder.setContentText("Rest timer is up, start your set!");
builder.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE);
builder.setAutoCancel(true);
Intent intent = new Intent(getActivity(), WorkoutDaysActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getContext());
stackBuilder.addParentStack(WorkoutDaysActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_NO_CREATE);
builder.setContentIntent(pendingIntent);
NotificationManager NM = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
NM.notify(0, builder.build());
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
SharedPreferences pref = this.getActivity().getSharedPreferences("MyPref", 0);
View view = inflater.inflate(R.layout.monday_fragment, null);
txtViewRest = (TextView) view.findViewById(R.id.txtViewRest);
btnStart = (Button) view.findViewById(R.id.btnStart);
btnStop = (Button) view.findViewById(R.id.btnStop);
btnReset = (Button) view.findViewById(R.id.btnReset);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
start();
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cancel();
}
});
btnReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
reset();
}
});
任何和所有的帮助是非常感谢!
我设法解决了这个问题,我将在这里发布解决方案,以防将来有人遇到同样的问题。
这实际上是一个非常简单的修复,我所要做的就是在通知代码中添加额外的意图。
从mywork。java中,我将这个意图传递给WorkoutDaysActivity:
Intent intent = new Intent(getApplicationContext(), WorkoutDaysActivity.class);
intent.putExtra("workout", "w29w1");
startActivity(intent);
在workout days活动中,我有这个意图,并将其作为参数传递给所有三个片段,如下所示:
Intent intent = getIntent();
String workout = intent.getStringExtra("workout");
Bundle args = new Bundle();
args.putString("workout", workout);
MondayFragment mondayFragment = new MondayFragment();
mondayFragment.setArguments(args);
adapter.addPage(mondayFragment);
WedFragment wedFragment = new WedFragment();
wedFragment.setArguments(args);
adapter.addPage(wedFragment);
然后在我发出通知的每个片段中我必须添加intent。putextra
public void showNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext());
builder.setSmallIcon(R.drawable.sheikoicon);
builder.setContentTitle("Rest Timer");
builder.setContentText("Rest timer is up, start your set!");
builder.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE);
builder.setAutoCancel(true);
Intent intent = new Intent(getActivity(), WorkoutDaysActivity.class);
intent.putExtra("workout", workout3);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getContext());
stackBuilder.addParentStack(WorkoutDaysActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
NotificationManager NM = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
NM.notify(0, builder.build());
}
其中workout3为:
args = getArguments();
workout3 = args.getString("workout");