2012-12-21 46 views
1

我有一個通知欄,當我點擊它時,用相同的數據(一個字符串數組)開始一個新的活動。我有一個問題,從onReceive.class傳遞數據到新的活動(通知)。當我點擊我看到新的活動,但與nullPointerE崩潰。將數據從onReceive傳遞到新的活動

NOT1 [X]是串的陣列

我的代碼

AlarmReceiver:

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"); 

回答

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(","); 
+0

同樣的問題,同樣的崩潰。 –

+0

讓我爲你製作整個程序 – mjosh

0

這是not1[x]一個字符串數組?它看起來像一個String Array的元素,它實際上是一個String。

0

只需在AlarmReceiver類中替換下面的代碼即可。其作品對我來說...

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

如果我們用0而不是PendingIntent.FLAG_UPDATE_CURRENT標誌,在接收方即getIntent()。getExtras()不是動態更新的數據,分配1日傳遞的數據每時間....

相關問題