0

这是我创建 PendingIntent 的代码:

Bundle b = new Bundle();
b.putCharSequence("Dest", myTrip.inboundFlights.get(i).getDestination());
b.putCharSequence("Orig", myTrip.inboundFlights.get(i).getOrigin());
b.putLong("DepTime", myTrip.inboundFlights.get(i).getEtd().getTime());
Intent myIntent = new Intent("NotificationActivity");
myIntent.putExtras(b);
System.out.println(myIntent.getExtras());
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

根据我发现的所有内容,这应该可以工作,但是当我尝试访问活动中的捆绑包时,它是 NULL。

这是活动:

public class NotificationActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    System.out.println(savedInstanceState);

    NotificationManager mNotificationManager;
    mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    Context context = getApplicationContext();
    int icon = R.drawable.app_icon;
    CharSequence tickerText = "tickerText";
    CharSequence contentTitle = "title";

    CharSequence contentText = savedInstanceState.getCharSequence("Dest"); //Here's the crash with null pointer exception
    Intent notificationIntent = new Intent(this, NotificationActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    long when = savedInstanceState.getLong("DepTime");
    int id = (int) savedInstanceState.getLong("DepTime");//temporary will change later
    Notification notification = new Notification(icon, tickerText, when);
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

    mNotificationManager.notify( id, notification);
    finish();

}
}

这是 AndroidManifest.xml 中用于活动的部分:

<activity android:name=".Helpers.NotificationActivity" >
        <intent-filter>
            <action android:name="NotificationActivity" />
            <category android:name="android.intent.category.DEFAULT" /> 
        </intent-filter>
    </activity>

我错过了什么?

4

1 回答 1

3

我想你可能想试试

Bundle data = getIntent().getExtras();
CharSequence contentText = data.getCharSequence("Dest");

代替

CharSequence contentText = savedInstanceState.getCharSequence("Dest");
于 2012-08-21T18:31:46.643 回答