android - EditText getText crashes -
seemingly simple exercise, yet somehow crashes program. trying retrieve string user enters edittext field. when hit submit button, crash. again!
here code , logcat info:
public class findeventsactivity extends activity implements locationlistener { geopoint searchfrom; private static final int use_current_coordinates = 0; private static final int enter_city = 1; private static final int enter_zip = 2; private static final int use_map = 3; private static final int result_from_map = 1; context context; string citystate; geocoder geocoder; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_find_events); context = this; setclicklisteners(); } private void setclicklisteners() { relativelayout locationbox = (relativelayout) findviewbyid(r.id.locationbox); locationbox.setonclicklistener(new view.onclicklistener(){ @override public void onclick(view v) { final charsequence[] methods = {"use current location", "enter city / state" , "enter zip code", "use map"}; alertdialog.builder chooser = new alertdialog.builder(findeventsactivity.this); chooser.settitle("choose search location method").setitems(methods, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int position) { switch (position) { case use_current_coordinates: searchfrom = getcurrentcoordinates(); break; case enter_city: searchfrom = getfromcitystate(); break; } } }); chooser.show(); } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.find_events, menu); return true; } private geopoint getcurrentcoordinates() { if (isgoogleplay()) { location lastknownlocation = getlastknownlocation(); int lat = (int) (lastknownlocation.getlatitude() * 1e6); int lng = (int) (lastknownlocation.getlongitude() * 1e6); return new geopoint(lat, lng); } return null; } public void submitbuttononclick(view view) { system.err.println("inside ontouch"); edittext city_entry = (edittext) view.findviewbyid(r.id.city_entry); spinner state = (spinner) findviewbyid(r.id.state_spinner); system.err.println("here"); system.err.println(city_entry.gettext().tostring()); system.err.println("city string captured"); system.err.println(state.getselecteditem().tostring()); citystate = city_entry.gettext().tostring() + ", " + state.getselecteditem().tostring(); toast.maketext(getapplicationcontext(), citystate, toast.length_long).show(); } private geopoint getfromcitystate() { layoutinflater inflater = layoutinflater.from(this); view view = inflater.inflate(r.layout.city_state_entry, null); alertdialog.builder builder = new alertdialog.builder(this); builder.setview(view); //setcontentview(r.layout.city_state_entry); //button submitbutton = (button) findviewbyid(r.id.submitbutton); system.err.println("about create onclicklistener"); // create alert dialog alertdialog alertdialog = builder.create(); alertdialog.show(); return null; } @override public void onlocationchanged(location arg0) { // todo auto-generated method stub } @override public void onproviderdisabled(string arg0) { // todo auto-generated method stub } @override public void onproviderenabled(string arg0) { // todo auto-generated method stub } @override public void onstatuschanged(string arg0, int arg1, bundle arg2) { // todo auto-generated method stub } private location getlastknownlocation() { locationmanager locationmanager = (locationmanager) getsystemservice(location_service); /* verify gps turned on */ if ( !locationmanager.isproviderenabled( locationmanager.gps_provider ) ) { buildalertmessagenogps(); } criteria criteria = new criteria(); criteria.setaccuracy(criteria.accuracy_fine); criteria.setaltituderequired(false); criteria.setbearingrequired(false); criteria.setcostallowed(false); criteria.setpowerrequirement(criteria.power_high); string provider = locationmanager.getbestprovider(criteria, true); if (provider == null) { onproviderdisabled(null); } location location = locationmanager.getlastknownlocation(provider); locationmanager.requestlocationupdates(locationmanager.gps_provider, 300000, 5000, this); if (location != null) { system.out.println("provider " + provider + " has been selected."); onlocationchanged(location); return location; } return null; } private void buildalertmessagenogps() { final alertdialog.builder builder = new alertdialog.builder(this); builder.setmessage("your gps seems disabled, want enable it?") .setcancelable(false) .setpositivebutton("yes", new dialoginterface.onclicklistener() { public void onclick(@suppresswarnings("unused") final dialoginterface dialog, @suppresswarnings("unused") final int id) { startactivity(new intent(android.provider.settings.action_location_source_settings)); } }) .setnegativebutton("no", new dialoginterface.onclicklistener() { public void onclick(final dialoginterface dialog, @suppresswarnings("unused") final int id) { dialog.cancel(); } }); final alertdialog alert = builder.create(); alert.show(); } private boolean isgoogleplay() { /* function checks make sure google play service installed on user's phone */ int status = googleplayservicesutil.isgoogleplayservicesavailable(this); if (status != connectionresult.success) { ((dialog) googleplayservicesutil.geterrordialog(status, this, 10)).show(); //toast.maketext(this, "google play services not available. check setup.", toast.length_short).show(); return false; } return true; } } <?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/city_state_entry" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="@drawable/background" android:padding="10dp" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/city_state_title" android:textcolor="#000000" android:layout_gravity="center_horizontal" android:paddingbottom="10dip" /> <textview android:id="@+id/enter_city" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/enter_city" /> <edittext android:id="@+id/city_entry" android:inputtype="textcapwords" android:layout_width="match_parent" android:layout_height="wrap_content" > <requestfocus /> </edittext> <textview android:id="@+id/enter_state" android:layout_margintop="10dip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/select_state" /> <spinner android:id="@+id/state_spinner" android:layout_width="match_parent" android:layout_height="wrap_content" android:entries="@array/state_abbreviations" /> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="20dip" android:layout_gravity="center_horizontal" android:paddingbottom="10dp"> <button android:id="@+id/submitbutton" style="@style/buttontext" android:onclick="submitbuttononclick" android:layout_gravity="center_horizontal" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_marginbottom="10dp" android:background="@drawable/blue_button" android:text="@string/submit" /> </linearlayout> </linearlayout> 05-19 19:23:56.703: w/system.err(4744): inside ontouch 05-19 19:23:56.703: w/system.err(4744): here 05-19 19:23:56.703: d/androidruntime(4744): shutting down vm 05-19 19:23:56.703: w/dalvikvm(4744): threadid=1: thread exiting uncaught exception (group=0x4001d5a0) 05-19 19:23:56.713: e/androidruntime(4744): fatal exception: main 05-19 19:23:56.713: e/androidruntime(4744): java.lang.illegalstateexception: not execute method of activity 05-19 19:23:56.713: e/androidruntime(4744): @ android.view.view$1.onclick(view.java:2191) 05-19 19:23:56.713: e/androidruntime(4744): @ android.view.view.performclick(view.java:2532) 05-19 19:23:56.713: e/androidruntime(4744): @ android.view.view$performclick.run(view.java:9293) 05-19 19:23:56.713: e/androidruntime(4744): @ android.os.handler.handlecallback(handler.java:587) 05-19 19:23:56.713: e/androidruntime(4744): @ android.os.handler.dispatchmessage(handler.java:92) 05-19 19:23:56.713: e/androidruntime(4744): @ android.os.looper.loop(looper.java:150) 05-19 19:23:56.713: e/androidruntime(4744): @ android.app.activitythread.main(activitythread.java:4369) 05-19 19:23:56.713: e/androidruntime(4744): @ java.lang.reflect.method.invokenative(native method) 05-19 19:23:56.713: e/androidruntime(4744): @ java.lang.reflect.method.invoke(method.java:507) 05-19 19:23:56.713: e/androidruntime(4744): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:846) 05-19 19:23:56.713: e/androidruntime(4744): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:604) 05-19 19:23:56.713: e/androidruntime(4744): @ dalvik.system.nativestart.main(native method) 05-19 19:23:56.713: e/androidruntime(4744): caused by: java.lang.reflect.invocationtargetexception 05-19 19:23:56.713: e/androidruntime(4744): @ java.lang.reflect.method.invokenative(native method) 05-19 19:23:56.713: e/androidruntime(4744): @ java.lang.reflect.method.invoke(method.java:507) 05-19 19:23:56.713: e/androidruntime(4744): @ android.view.view$1.onclick(view.java:2186) 05-19 19:23:56.713: e/androidruntime(4744): ... 11 more 05-19 19:23:56.713: e/androidruntime(4744): caused by: java.lang.nullpointerexception 05-19 19:23:56.713: e/androidruntime(4744): @ com.mobilenicity.find_events.findeventsactivity.submitbuttononclick(findeventsactivity.java:85) 05-19 19:23:56.713: e/androidruntime(4744): ... 14 more
the edittext part of dialog, , therefore not child of activity, nor child of pressed button (which view argument of onclick method.
so need get/keep hold of it, eg assigning field of activity (quick&dirty). in turn give issues on rotation etc, that's topic. making field should somewhere.
Comments
Post a Comment