我使用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);
正如你所看到的,我的意图中有一个Extra:“extrastring”。
现在,当我尝试从已启动的活动中读取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
答案 0 :(得分:4)
我一直在使用Bundle
做类似的事情final Intent notificationIntent = new Intent(context, A.class);
notificationIntent.putExtras(extra);
final PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
其他是捆绑包:
final Bundle extra = new Bundle();
extra.putString("Message", "hello");
您可以在onCreate of started Activity中检索Bundle的值:
final Bundle extra = getIntent().getExtras();
if (extra != null) {
String message=extra.getString("Message");
}
此外,您可以使用标记PendingIntent.FLAG_UPDATE_CURRENT
此标志的作用如下:
如果描述的PendingIntent已经存在,那么保留它,但是它用这个新Intent中的内容替换它的额外数据。如果您要创建只有附加内容更改的意图,并且不关心接收到您之前的PendingIntent的任何实体将能够使用您的新附加功能启动它,即使它们未明确赋予它,也可以使用此功能。
答案 1 :(得分:1)
您可能想要做的是将该字符串放入Bundle
,并将其添加到您的Intent(使用putExtras()。
然后你可以打电话给你getExtras(),让你的捆绑回来,你可以从中获得你的回报。
答案 2 :(得分:1)
试试这个:
Bundle extras = getIntent().getExtras();
String message = extras.getString("extrastring");
答案 3 :(得分:1)
试试这种方式
String result = getIntent().getExtras().getString("extrastring");
答案 4 :(得分:1)
根据您的描述,我怀疑导致NullPointerException
是因为getIntent()
methiod返回null
。可能有两种可能的原因发生:
getIntent()
方法被触发之前,请检查您是否未调用onCreate()
。希望有所帮助!