android - Nullpointer exception when starting another activity -
my program contains 2 activities.i called second activity method showevent .but error occured, nullpointer exception.why ? program contains 2 activities.i called second activity method showevent .but error occured, nullpointer exception.why this
firstactivity.java
public class firstactivity extends fragmentactivity implements onitemselectedlistener { /** called when activity first created. */ public final static string extra_message = "com.example.myfirstapp.message"; public final static string extra_message1 = "com.example.myfirstapp.message"; classdbopenhelper eventsdata; textview userselection; button okbutton; button addbutton; button change_date_but; textview date; textview show; edittext edittext; public static final int date_dialog_id = 1; private int myear; private int mmonth; private int mday; private static final string[] items={"yalahanka","rajaji nagar","sivaji nagar","koramangala","rt nagar", "banashankari","yashwanthpura","hebbal"}; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_first); okbutton = (button)findviewbyid(r.id.button2); addbutton = (button)findviewbyid(r.id.button3); change_date_but = (button)findviewbyid(r.id.button1); date = (textview)findviewbyid(r.id.textview2);//key_date userselection=(textview)findviewbyid(r.id.textview1);//key_desc edittext=(edittext)findviewbyid(r.id.edittext1);//key_event show=(textview)findviewbyid(r.id.textview5); spinner my_spin=(spinner)findviewbyid(r.id.spinner1); my_spin.setonitemselectedlistener(this); arrayadapter aa=new arrayadapter(this, android.r.layout.simple_spinner_item,items); aa.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); my_spin.setadapter(aa); okbutton.setonclicklistener(new clicker()); eventsdata = new classdbopenhelper(this); change_date_but.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { datepickerdialog dpd = new datepickerdialog( firstactivity.this, mdatesetlistener, myear, mmonth,mday); dpd.show(); } }); final calendar c = calendar.getinstance(); myear = c.get(calendar.year); mmonth = c.get(calendar.month); mday = c.get(calendar.day_of_month); updatedisplay(); } @override @deprecated protected void onpreparedialog(int id, dialog dialog) { // todo auto-generated method stub super.onpreparedialog(id, dialog); ((datepickerdialog) dialog).updatedate(myear, mmonth, mday); } private datepickerdialog.ondatesetlistener mdatesetlistener = new datepickerdialog.ondatesetlistener() { public void ondateset(datepicker view, int year, int monthofyear, int dayofmonth) { myear = year; mmonth = monthofyear; mday = dayofmonth; updatedisplay(); } }; private void updatedisplay() { // todo auto-generated method stub date.settext(new stringbuilder() // month 0 based add 1 .append(mmonth + 1).append("-").append(mday).append("-") .append(myear)); } @override public void onitemselected(adapterview<?> arg0, view arg1, int pos, long arg3) { userselection.settext(items[pos]); } @override public void onnothingselected(adapterview<?> arg0) { // todo auto-generated method stub userselection.settext(""); } class clicker implements button.onclicklistener { public void onclick(view v) { string datevalue = date.gettext().tostring(); string userselectvalue = userselection.gettext().tostring(); string userevent = edittext.gettext().tostring(); sqlitedatabase db = eventsdata.getwritabledatabase(); contentvalues cv = new contentvalues(); cv.put(classdbopenhelper.key_desc, userselectvalue); cv.put(classdbopenhelper.key_event, userevent); cv.put(classdbopenhelper.key_date,datevalue); db.insert(classdbopenhelper.database_table, null, cv); db.close(); } } public void showevent(view view){ intent intent = new intent(this, getclicker.class); date = (textview)findviewbyid(r.id.textview2); string datevalue = date.gettext().tostring(); userselection=(textview)findviewbyid(r.id.textview1); string userselectvalue = userselection.gettext().tostring(); intent.putextra(extra_message, datevalue); intent.putextra(extra_message1, userselectvalue); startactivity(intent); } }
getclicker.java
package example.events1; import android.app.listactivity; import android.content.intent; import android.database.cursor; import android.os.bundle; import android.support.v4.widget.simplecursoradapter; import android.widget.listview; public class getclicker extends listactivity { intent intent = getintent(); string datevalue = intent.getstringextra(firstactivity.extra_message); string userselectvalue = intent.getstringextra(firstactivity.extra_message1); classdbopenhelper eventsdata1; cursor cursor1; listview listview ; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_second); /*string datevalue = date.gettext().tostring(); string userselectvalue = userselection.gettext().tostring(); string showevent = eventsdata.getcontact(datevalue,userselectvalue); show.settext(showevent);*/ cursor1 = eventsdata1.getcontact(datevalue,userselectvalue); string[] fromcolumns = {classdbopenhelper.key_event}; int[] toviews = {r.id.event}; simplecursoradapter adapter = new simplecursoradapter(this,r.layout.events, cursor1, fromcolumns, toviews,0 ); listview = (listview) findviewbyid(android.r.id.list); listview.setadapter(adapter); } public void ondestroy() { eventsdata1.close(); } }
logcat :
08-07 10:00:58.396: e/androidruntime(889): fatal exception: main 08-07 10:00:58.396: e/androidruntime(889): java.lang.runtimeexception: unable instantiate activity componentinfo{example.events1/example.events1.getclicker}: java.lang.nullpointerexception 08-07 10:00:58.396: e/androidruntime(889): @ android.app.activitythread.performlaunchactivity(activitythread.java:2106) 08-07 10:00:58.396: e/androidruntime(889): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230) 08-07 10:00:58.396: e/androidruntime(889): @ android.app.activitythread.access$600(activitythread.java:141) 08-07 10:00:58.396: e/androidruntime(889): @ android.app.activitythread$h.handlemessage(activitythread.java:1234) 08-07 10:00:58.396: e/androidruntime(889): @ android.os.handler.dispatchmessage(handler.java:99) 08-07 10:00:58.396: e/androidruntime(889): @ android.os.looper.loop(looper.java:137) 08-07 10:00:58.396: e/androidruntime(889): @ android.app.activitythread.main(activitythread.java:5041) 08-07 10:00:58.396: e/androidruntime(889): @ java.lang.reflect.method.invokenative(native method) 08-07 10:00:58.396: e/androidruntime(889): @ java.lang.reflect.method.invoke(method.java:511) 08-07 10:00:58.396: e/androidruntime(889): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 08-07 10:00:58.396: e/androidruntime(889): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 08-07 10:00:58.396: e/androidruntime(889): @ dalvik.system.nativestart.main(native method) 08-07 10:00:58.396: e/androidruntime(889): caused by: java.lang.nullpointerexception 08-07 10:00:58.396: e/androidruntime(889): @ example.events1.getclicker.<init>(getclicker.java:12) 08-07 10:00:58.396: e/androidruntime(889): @ java.lang.class.newinstanceimpl(native method) 08-07 10:00:58.396: e/androidruntime(889): @ java.lang.class.newinstance(class.java:1319) 08-07 10:00:58.396: e/androidruntime(889): @ android.app.instrumentation.newactivity(instrumentation.java:1054) 08-07 10:00:58.396: e/androidruntime(889): @ android.app.activitythread.performlaunchactivity(activitythread.java:2097) 08-07 10:00:58.396: e/androidruntime(889): ... 11 more
move these:
intent intent = getintent(); string datevalue = intent.getstringextra(firstactivity.extra_message); string userselectvalue = intent.getstringextra(firstactivity.extra_message1);
into oncreate()
method. guess because intent havent been there yet.
hope helps.
Comments
Post a Comment