java - In Activity.onCreate(), why does Intent.getExtras() sometimes return null? -
this false alarm, see my own answer. original question below:
an activity has button takes user activity. launch new activity, populate our intent extras, , oncreate(), new activity reads extras via intent.getextras(). assumed returned bundle non-null, customer crash reports discovered, getextras() returns null.
null-guarding extras, this answer shows, fine, if populate intent's extras, why ever return null later? there better place (like onresume()) read extras?
edit: may because not following name convention required keys:
the name must include package prefix, example app com.android.contacts use names "com.android.contacts.showall".
this intent.putextras javadoc. happens if don't follow name convention; behavior defined?
here's relevant code:
class fromactivity extends activity { imagebutton button; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.from_view); button = (imagebutton)findviewbyid(r.id.button); button.setvisibility(view.visible); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent = new intent(fromactivity.this, toactivity.class); i.putextra(toactivity.server_param, ...); i.putextra(toactivity.uuid_param, ...); i.putextra(toactivity.template_param, ...); startactivityforresult(i, 0); overridetransition(r.anim.slide_left_in, r.anim.slide_left_out); } }); } } class toactivity extends activity { public static final string server_param = "server"; public static final string uuid_param = "uuid"; public static final string template_param = "template"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); bundle extras = getintent().getextras(); if (extras == null) { finish(); return; } // stuff extras } }
here sample stack trace of problem:
java.lang.runtimeexception: unable start activity componentinfo{toactivity}: java.lang.nullpointerexception @ android.app.activitythread.performlaunchactivity(activitythread.java:2355) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2391) @ android.app.activitythread.access$600(activitythread.java:151) @ android.app.activitythread$h.handlemessage(activitythread.java:1335) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:155) @ android.app.activitythread.main(activitythread.java:5493) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1028) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:795) @ dalvik.system.nativestart.main(native method) caused by: java.lang.nullpointerexception @ toactivity.oncreate(sourcefile:49) @ android.app.activity.performcreate(activity.java:5066) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1101) @ android.app.activitythread.performlaunchactivity(activitythread.java:2311) ... 11 more java.lang.nullpointerexception @ toactivity.oncreate(sourcefile:49) @ android.app.activity.performcreate(activity.java:5066) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1101) @ android.app.activitythread.performlaunchactivity(activitythread.java:2311) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2391) @ android.app.activitythread.access$600(activitythread.java:151) @ android.app.activitythread$h.handlemessage(activitythread.java:1335) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:155) @ android.app.activitythread.main(activitythread.java:5493) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1028) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:795) @ dalvik.system.nativestart.main(native method)
you can this:
getintent().getstringextra(key);
or:
getintent().getextras().getstring(key)
and set in "fromactivity":
bundle extras = new bundle(); extras.putstring(key, value); intent.putextras(extras); //and start activity...
or:
intent.putextra(key, string); //and start activity...
either way should work, hope helps...
regards!
Comments
Post a Comment