1

我有一个通知栏,当我单击它时,会使用相同的数据(字符串数组)启动一个新活动。我在将数据从 onReceive.class 传递到新活动(通知)时遇到问题。当我单击时,我看到了新活动,但由于 nullPointerE 而崩溃。

not1[x] 是一个字符串数组

我的代码

报警接收器:

Intent notificationIntent = new Intent("com.example.myapp", null, context, Notify.class);
        notificationIntent.putExtra("notify", not1[x]);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

通知:

Bundle b = getIntent().getExtras();
      if (b != null) {
          Intent intent = getIntent();
          String[] notify2 = b.getStringArray("notify");

          textView1.setText("notify2");
4

3 回答 3

1

尝试这个

String[] getArray=getIntent().getStringArrayExtra("notify");

在 not1[x] 之间是一个字符串而不是一个数组。

private NotificationManager mNotificationManager;
    private int SIMPLE_NOTFICATION_ID;

mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    notifyDetails = new Notification(R.drawable.somepngfile,"some text",System.currentTimeMillis());


       Context context = getApplicationContext();
        CharSequence contentTitle = "title";
        CharSequence contentText = "ontent";
        Intent notifyIntent = new Intent(thisclass.this,nextclass.class);

        notifyIntent.putExtra("Value", finalresponse[1]+","+finalresponse[2]+","+finalresponse[3]);



        PendingIntent intent = 
            PendingIntent.getActivity(BackService.this, 0, 
            notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);

        notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);
        notifyDetails.defaults|= Notification.DEFAULT_SOUND;
        notifyDetails.defaults|= Notification.DEFAULT_LIGHTS;
        notifyDetails.defaults|= Notification.DEFAULT_VIBRATE;
        notifyDetails.defaults|= Notification.FLAG_AUTO_CANCEL;
        notifyDetails.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
        mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);

在下节课

 Intent i = getIntent();
        i.getStringExtra("Value");

        String[] response=i.getStringExtra("Value").split(",");
于 2012-12-21T18:28:19.243 回答
0

这是not1[x]一个字符串数组吗?它看起来像一个字符串数组的元素,实际上是一个字符串。

于 2012-12-21T20:41:29.030 回答
0

只需替换AlarmReceiver类中的以下代码即可。它对我有用...

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

如果我们使用 0 而不是PendingIntent.FLAG_UPDATE_CURRENT标志,在接收方,即 getIntent().getExtras() 不会动态更新数据,每次都分配第一个传递的数据......

于 2013-11-19T12:42:08.537 回答