Monday, 15 March 2010

java - gridview setOnItemClickListener not working -



java - gridview setOnItemClickListener not working -

i'm using gridview display numbers, want next activity, when click on number when click on n number nil happens there no error in logcat nor forcefulness close.

here code

public class cactivity extends activity { gridview gridview; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.cactivity); gridview.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> parent, view v, int position, long id) { intent myintent = new intent(getapplicationcontext(), abc.class); startactivity(myintent); //finish(); } }); } private void initcomponent() { gridview = (gridview) findviewbyid(r.id.month_gridview); }

i don't know problem gridview.setadapter(adapter); working. plz help

gridview.setadapter(adapter);

if utilize

gridview gridview = (gridview) findviewbyid(r.id.month_gridview);

then app forcefulness closes

logcat

02-07 21:10:12.721: w/dalvikvm(719): threadid=1: thread exiting uncaught exception (group=0x40015560) 02-07 21:10:12.780: e/androidruntime(719): fatal exception: main 02-07 21:10:12.780: e/androidruntime(719): java.lang.runtimeexception: unable instantiate activity componentinfo{com.indianic.demo.calendark/com.indianic.demo.calendark.calendaractivity}: java.lang.nullpointerexception 02-07 21:10:12.780: e/androidruntime(719): @ android.app.activitythread.performlaunchactivity(activitythread.java:1569) 02-07 21:10:12.780: e/androidruntime(719): @ android.app.activitythread.handlelaunchactivity(activitythread.java:1663) 02-07 21:10:12.780: e/androidruntime(719): @ android.app.activitythread.access$1500(activitythread.java:117) 02-07 21:10:12.780: e/androidruntime(719): @ android.app.activitythread$h.handlemessage(activitythread.java:931) 02-07 21:10:12.780: e/androidruntime(719): @ android.os.handler.dispatchmessage(handler.java:99) 02-07 21:10:12.780: e/androidruntime(719): @ android.os.looper.loop(looper.java:123) 02-07 21:10:12.780: e/androidruntime(719): @ android.app.activitythread.main(activitythread.java:3683) 02-07 21:10:12.780: e/androidruntime(719): @ java.lang.reflect.method.invokenative(native method) 02-07 21:10:12.780: e/androidruntime(719): @ java.lang.reflect.method.invoke(method.java:507) 02-07 21:10:12.780: e/androidruntime(719): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 02-07 21:10:12.780: e/androidruntime(719): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:597) 02-07 21:10:12.780: e/androidruntime(719): @ dalvik.system.nativestart.main(native method) 02-07 21:10:12.780: e/androidruntime(719): caused by: java.lang.nullpointerexception 02-07 21:10:12.780: e/androidruntime(719): @ android.app.activity.findviewbyid(activity.java:1647) 02-07 21:10:12.780: e/androidruntime(719): @ com.indianic.demo.calendark.calendaractivity.<init>(calendaractivity.java:44) 02-07 21:10:12.780: e/androidruntime(719): @ java.lang.class.newinstanceimpl(native method) 02-07 21:10:12.780: e/androidruntime(719): @ java.lang.class.newinstance(class.java:1409) 02-07 21:10:12.780: e/androidruntime(719): @ android.app.instrumentation.newactivity(instrumentation.java:1021) 02-07 21:10:12.780: e/androidruntime(719): @ android.app.activitythread.performlaunchactivity(activitythread.java:1561) 02-07 21:10:12.780: e/androidruntime(719): ... 11 more 02-07 21:10:18.061: i/process(719): sending signal. pid: 719 sig: 9

i used initcomponent() method before setting setonitemclicklistener()

the forcefulness close error gone , app opens normally,

but next activity not opening or nil happens.when click on items. :(

as said tried different cases,still no success

public void onitemclick(adapterview<?> parent, view v3, int position, long id) { //setcontentview(r.layout.abc); switch (position) { case 0: intent myintent = new intent(getapplicationcontext(),abc.class); startactivity(myintent); break; case 1: intent myintent1 = new intent(getapplicationcontext(),abc.class); startactivity(myintent1); break; case 2: intent myintent2 = new intent(getapplicationcontext(),abc.class); startactivity(myintent2); break; case 3: intent myintent3 = new intent(getapplicationcontext(),abc.class); startactivity(myintent3); break; case 4: intent myintent4 = new intent(getapplicationcontext(),abc.class); startactivity(myintent4); break; case 5: intent myintent5 = new intent(getapplicationcontext(),abc.class); startactivity(myintent5); break; case 6: intent myintent6 = new intent(getapplicationcontext(),abc.class); startactivity(myintent6); break; default: break; } } });

you need phone call custom initcomponent() method before setting setonitemclicklistener()

public class cactivity extends activity { gridview gridview; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.cactivity); initcomponent(); gridview.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> parent, view v, int position, long id) { switch (position) { case 0://do same remaining items intent myintent = new intent(getapplicationcontext(), abc.class); startactivity(myintent); break; default: break; } } //finish(); } }); } private void initcomponent() { gridview = (gridview) findviewbyid(r.id.month_gridview); }

java android

No comments:

Post a Comment