我尝试通过存储在通知中的意图传递String。 但是当我从新片段的意图中获取String时,它是一个null并且它给了我一个NullPointerException。
创建通知的代码(在活动A中的片段A的方法A中)
Notification.Builder mBuilder = new Notification.Builder(getActivity().getBaseContext());
mBuilder.setContentTitle("My first notification");
mBuilder.setContentText("Click to see the details...");
mBuilder.setSmallIcon(R.drawable.ic_launcher);
//Intent maken voor notification
Intent intentVoorNotificatie = new Intent(getActivity(), activity_detail_notification.class);
EditText edTekst = (EditText) getActivity().findViewById(R.id.etTekst);
String strTekstNotificatie = edTekst.getText().toString();
intentVoorNotificatie.putExtra("detailNotificatie", strTekstNotificatie);
//Intent naar PendingIntent converteren
PendingIntent pIntentVootNotificatie = PendingIntent.getActivity(getActivity().getBaseContext(),
0, intentVoorNotificatie, PendingIntent.FLAG_ONE_SHOT);
mBuilder.setContentIntent(pIntentVootNotificatie);
//Notificatie tonen
NotificationManager mNotificationManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1,mBuilder.build());
活动B中片段B的方法A
//Haal doorgegeven tekst uit intent
Intent intent = getActivity().getIntent();
String strTekst = intent.getStringExtra("detailNotificatie");
TextView tvTekst = (TextView) getActivity().findViewById(R.id.tvDetailVanNotificatie);
tvTekst.setText(strTekst);
我在setText()
方法上得到nullPointer,因为当从意图中提取String时,String为空。
我是否因为数据丢失或被解雇而做错了什么?
答案 0 :(得分:0)
尝试更改此代码`
PendingIntent pIntentVootNotificatie = PendingIntent.getActivity(getActivity().getBaseContext(),
0, intentVoorNotificatie, PendingIntent.FLAG_ONE_SHOT);
到这个`
PendingIntent pIntentVootNotificatie = PendingIntent.getActivity(getActivity().getBaseContext(),
0, intentVoorNotificatie, PendingIntent.FLAG_UPDATE_CURRENT);