【问题标题】:Android intent via notification not keeping extra data通过通知的Android意图不保留额外数据
【发布时间】: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


    【解决方案1】:

    尝试更改此代码`

     PendingIntent pIntentVootNotificatie = PendingIntent.getActivity(getActivity().getBaseContext(),
                                                    0, intentVoorNotificatie, PendingIntent.FLAG_ONE_SHOT);
    

    到这个`

    PendingIntent pIntentVootNotificatie = PendingIntent.getActivity(getActivity().getBaseContext(),
                                                    0, intentVoorNotificatie, PendingIntent.FLAG_UPDATE_CURRENT);
    

    【讨论】:

    • 这不起作用,但我找到了问题的根源。当我从我的设计中获得我的 TextView 时,它似乎不起作用。它仍然等于 null,这就是 SetText() 方法不起作用的原因。现在试图弄清楚为什么它给了我一个 null
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-21
    相关资源
    最近更新 更多