android - getExtras NullPointerException -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
app crash nullpointerexception in line int position = i.getextras().getint("id"); problem ? tell me please !
i build on tutorial http://seegatesite.com/android-tutorial-display-image-to-android-gridview-from-url-using-picasso
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final gridview gridview = (gridview) findviewbyid(r.id.gridview); gridview.setadapter(new imageadapter(this)); gridview.setonitemclicklistener(new adapterview.onitemclicklistener() { public void onitemclick(adapterview<?> parent, view v, int position, long id) { intent = new intent(getapplicationcontext(), fullimageactivity.class); i.putextra("id", position); startactivity(i);
fullimageactivity
public class fullimageactivity extends activity { imageview img; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.fullimageview); intent = getintent(); int position = i.getextras().getint("id"); imageadapter imageadapter = new imageadapter(this); img = (imageview) findviewbyid(r.id.image); string url = imageadapter.getitem(position); new downloadimage().execute(url); } private class downloadimage extends asynctask<string, void, bitmap> { @override protected bitmap doinbackground(string... url) { string imageurl = url[0]; bitmap bitmap = null; try { inputstream input = new java.net.url(imageurl).openstream(); bitmap = bitmapfactory.decodestream(input); } catch (exception e) { e.printstacktrace(); } return bitmap; } @override protected void onpostexecute(bitmap result) { img.setimagebitmap(result); } }
imageadapter
public class imageadapter extends baseadapter { private context mcontext; int imagetotal = 7; public static string[] mthumbids = { "http://i.imgur.com/kzqo.jpg", "http://i.imgur.com/87ke.jpg", "http://i.imgur.com/hej.jpg", "http://i.imgur.com/3d3.jpg", "http://i.imgur.com/wli.jpg", "http://i.imgur.com/pp.jpg", "http://i.imgur.com/ldt.jpg", }; public imageadapter(context c) { mcontext = c; } public int getcount() { return imagetotal; } @override public string getitem(int position) { return mthumbids[position]; } public long getitemid(int position) { return 0; } public view getview(int position, view convertview, viewgroup parent) { imageview imageview; if (convertview == null) { imageview = new imageview(mcontext); imageview.setlayoutparams(new gridview.layoutparams(220, 220)); imageview.setscaletype(imageview.scaletype.center_crop); imageview.setpadding(8, 8, 8, 8); } else { imageview = (imageview) convertview; } string url = getitem(position); picasso.with(mcontext) .load(url) .placeholder(r.drawable.loader) .fit() .centercrop().into(imageview); return imageview; } }
errors
java.lang.runtimeexception: unable start activity componentinfo{123.myapplication/123.myapplication.fullimageactivity}: java.lang.nullpointerexception @ android.app.activitythread.performlaunchactivity(activitythread.java:2295) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2349) @ android.app.activitythread.access$700(activitythread.java:159) @ android.app.activitythread$h.handlemessage(activitythread.java:1316) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:176) @ android.app.activitythread.main(activitythread.java:5419) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:525) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1046) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:862) @ dalvik.system.nativestart.main(native method) caused by: java.lang.nullpointerexception @ 123.myapplication.fullimageactivity.oncreate(fullimageactivity.java:30) @ android.app.activity.performcreate(activity.java:5372) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1104) @ android.app.activitythread.performlaunchactivity(activitythread.java:2257) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2349) @ android.app.activitythread.access$700(activitythread.java:159) @ android.app.activitythread$h.handlemessage(activitythread.java:1316) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:176) @ android.app.activitythread.main(activitythread.java:5419) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:525) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1046) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:862) @ dalvik.system.nativestart.main(native method)
try out following code:
replace :
intent intent = new intent(getapplicationcontext(), fullimageactivity.class);
with this:
intent intent = new intent(mainactivity.this, fullimageactivity.class);
and then:
intent = getintent(); bundle extras = i.getextras(); int position = extras.getint("id");
Comments
Post a Comment