我使用 AlarmManager 通过 PendingIntent 启动活动。
Intent smon = new Intent(ctxt, VideoActivty.class);
final Bundle extra = new Bundle();
extra.putString("extrastring","monday");
smon.putExtras(extra);
//smon.putExtra("extrastring","monday");
smon.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent psmon = PendingIntent.getActivity(ctxt, 0, smon, 0);
Calendar calSet1 = Calendar.getInstance();
calSet1.set(Calendar.MONTH, c.get(Calendar.MONTH));
calSet1.set(Calendar.YEAR, c.get(Calendar.YEAR));
calSet1.set(Calendar.DAY_OF_WEEK, 2);
calSet1.set(Calendar.HOUR_OF_DAY, hsmon);
calSet1.set(Calendar.MINUTE, msmon);
calSet1.set(Calendar.SECOND, 0);
calSet1.set(Calendar.MILLISECOND, 0);
int delay1=0;
if(today.get(Calendar.DAY_OF_WEEK)>2)delay1=7 * 24 * 60 * 60 * 1000;
if((today.get(Calendar.DAY_OF_WEEK)==2)&&(today.get(Calendar.HOUR_OF_DAY)>hsmon))delay1=7 * 24 * 60 * 60 * 1000;
if((today.get(Calendar.DAY_OF_WEEK)==2)&&(today.get(Calendar.HOUR_OF_DAY)==hsmon)&&(today.get(Calendar.MINUTE)>msmon))delay1=7 * 24 * 60 * 60 * 1000;
Log.e("delay 1",Integer.toString(delay1));
//calSet.setTimeZone(TimeZone.getTimeZone("UTC"));
mgr.setRepeating(AlarmManager.RTC_WAKEUP, calSet1.getTimeInMillis()+delay1,
7 * 24 * 60 * 60 * 1000, psmon);
如您所见,我的 Intent 中有一个 Extra:“extrastring”。
现在,当我尝试从已启动的 Activity 中读取 Extra 时:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//
final Bundle extra = getIntent().getExtras();
String message="cazzo";
if (extra != null) {
message=extra.getString("extrastring");
}
Log.e("intent che è partito",message);
我得到一个 NullPointerException。
谢谢你的帮助
07-19 13:34:18.336: E/AndroidRuntime(14109): FATAL EXCEPTION: main
07-19 13:34:18.336: E/AndroidRuntime(14109): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.videdrome/com.example.videdrome.VideoActivty}: java.lang.NullPointerException: println needs a message
07-19 13:34:18.336: E/AndroidRuntime(14109): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-19 13:34:18.336: E/AndroidRuntime(14109): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-19 13:34:18.336: E/AndroidRuntime(14109): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-19 13:34:18.336: E/AndroidRuntime(14109): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-19 13:34:18.336: E/AndroidRuntime(14109): at android.os.Handler.dispatchMessage(Handler.java:99)
07-19 13:34:18.336: E/AndroidRuntime(14109): at android.os.Looper.loop(Looper.java:137)
07-19 13:34:18.336: E/AndroidRuntime(14109): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-19 13:34:18.336: E/AndroidRuntime(14109): at java.lang.reflect.Method.invokeNative(Native Method)
07-19 13:34:18.336: E/AndroidRuntime(14109): at java.lang.reflect.Method.invoke(Method.java:511)
07-19 13:34:18.336: E/AndroidRuntime(14109): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-19 13:34:18.336: E/AndroidRuntime(14109): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-19 13:34:18.336: E/AndroidRuntime(14109): at dalvik.system.NativeStart.main(Native Method)
07-19 13:34:18.336: E/AndroidRuntime(14109): Caused by: java.lang.NullPointerException: println needs a message
07-19 13:34:18.336: E/AndroidRuntime(14109): at android.util.Log.println_native(Native Method)
07-19 13:34:18.336: E/AndroidRuntime(14109): at android.util.Log.e(Log.java:231)
07-19 13:34:18.336: E/AndroidRuntime(14109): at com.example.videdrome.VideoActivty.onCreate(VideoActivty.java:41)
07-19 13:34:18.336: E/AndroidRuntime(14109): at android.app.Activity.performCreate(Activity.java:5104)
07-19 13:34:18.336: E/AndroidRuntime(14109): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-19 13:34:18.336: E/AndroidRuntime(14109): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-19 13:34:18.336: E/AndroidRuntime(14109): ... 11 more