【发布时间】:2014-02-15 19:32:36
【问题描述】:
我正在尝试通过存储在通知中的意图传递字符串。 但是当我从新片段中的意图中获取字符串时,它是一个空值,它给了我一个 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,因为从意图中提取它时字符串为空。
我是否做错了什么导致数据丢失或消失?
【问题讨论】:
标签: java android android-intent notifications