android - NullPointerException while retrieving edittext values in onPause in fragment -
android - NullPointerException while retrieving edittext values in onPause in fragment -
i have 3 tab fragments in viewpager. sending info 1st , 2nd fragment 3rd fragment. have implemented selection of images using custom gallery in 1st fragment(in on clicking imageview custom gallery activity displayed , selected images displayed in 1st fragment) have other form info retrieving in onpause() method , passing values 3rd fragment via interface(1st fragment->activity->3rd fragment gets data)
now when dont click on imageview(i.e dont go custom gallery activity) other form info passed 3rd fragment no issues.
but when click on imageview , visit custom gallery activity selecting images , coming 1st fragment..obviously onpause been called before going gallery activity.. handling exceptions using try-catch
but problem after other form datas have filled in 1st fragment not been passed 3rd fragment if fill them after select images first.
i null values in 3rd fragment when visit custom gallery activity 1st one.
i debugged values using log.. , found 1 of form info values printed @ next moment displayed "++log:entry corrupt or truncated"
i researched error , have found happens either when value big or when value null.. receiving invalid number format: invalid int "" in 3rd fragment trying 1 of form info integer values of 1st fragment.the log , above exception shows because getting null values in 3rd fragment..
please help me approach should use?to accomplish need. , since newbie in fragments going wrong somewhere..
this tabactivity --> submitpostactivity
bundle com.jbandroid; import java.util.arraylist; import java.util.list; import com.jbandroid.model.tabspageradapter; import android.app.activity; import android.os.bundle; import android.support.v4.app.fragmentactivity; import android.support.v4.app.fragmentmanager; import android.support.v4.view.viewpager; import android.support.v4.view.viewpager.onpagechangelistener; import android.util.log; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.app.actionbar; import android.app.actionbar.tab; import android.app.fragmenttransaction; import com.jbandroid.fragment.*; import com.jbandroid.gallery.customgallery; public class submitpostactivity extends fragmentactivity implements actionbar.tablistener,postinfofragment.setpostinfo,locationinfofragment.setlocationinfo{ private viewpager viewpager; private actionbar actionbar; private tabspageradapter madapter; fragmentmanager manager; personalinfofragment frag; //tab titles private string[] tabs = {"post info" , "location info" , "personal info" }; //defining form input variables /*private string post_title,post_desc,post_start_date,post_end_date,post_country,post_city,post_area, post_user_name,post_user_email,post_user_phone; private int post_category_id, post_image,post_price, post_quantity;*/ @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.action_submit_post); viewpager = (viewpager) findviewbyid(r.id.pager); actionbar = getactionbar(); manager = getsupportfragmentmanager(); madapter = new tabspageradapter(getsupportfragmentmanager()); //viewpager.setoffscreenpagelimit(2); viewpager.setadapter(madapter); //actionbar.sethomebuttonenabled(false); actionbar.setnavigationmode(actionbar.navigation_mode_tabs); (string tab : tabs){ actionbar.addtab(actionbar.newtab().settext(tab).settablistener(this)); } if(savedinstancestate != null){ actionbar.setselectednavigationitem( savedinstancestate.getint("tab",0)); } /** * on swiping viewpager create respective tab selected * */ viewpager.setonpagechangelistener(new onpagechangelistener() { @override public void onpageselected(int position) { // on changing page // create respected tab selected actionbar.setselectednavigationitem(position); } @override public void onpagescrolled(int arg0, float arg1, int arg2) { } @override public void onpagescrollstatechanged(int arg0) { } }); } @override protected void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); outstate.putint("tab", getactionbar().getselectednavigationindex()); } @override public void ontabselected(tab tab, fragmenttransaction ft) { // on tab selected // show respected fragment view viewpager.setcurrentitem(tab.getposition()); } @override public void pass_location_details(string country, string city, string area) { frag = (personalinfofragment) manager.findfragmentbytag("android:switcher:" + viewpager.getid() + ":" + 2); frag.get_post_location_details(country, city, area); log.d("submitarea", area); } @override public void pass_post_details(list<string> post_details) { frag = (personalinfofragment) manager.findfragmentbytag("android:switcher:" + viewpager.getid() + ":" + 2); frag.get_post_details(post_details); log.d("submitpostinfo","hello"+ post_details.get(5)); } }
this 1st fragment --> postinfofragment. have tried cutting short code.
bundle com.jbandroid.fragment; import java.util.arraylist; import java.util.list; import com.jbandroid.r; import com.jbandroid.submitpostactivity; import com.jbandroid.gallery.action; import com.jbandroid.gallery.customgallery; import com.jbandroid.gallery.customgalleryadapter; import com.nostra13.universalimageloader.cache.memory.impl.weakmemorycache; import com.nostra13.universalimageloader.core.displayimageoptions; import com.nostra13.universalimageloader.core.imageloader; import com.nostra13.universalimageloader.core.imageloaderconfiguration; import com.nostra13.universalimageloader.core.assist.imagescaletype; import android.support.v4.app.fragment; import android.app.actionbar; import android.app.activity; import android.content.context; import android.content.intent; import android.content.sharedpreferences; import android.content.res.resources; import android.content.res.typedarray; import android.graphics.bitmap; import android.os.bundle; import android.os.handler; import android.util.log; import android.view.layoutinflater; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.view.view.onclicklistener; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.adapterview.onitemselectedlistener; import android.widget.button; import android.widget.edittext; import android.widget.gridview; import android.widget.imageview; import android.widget.linearlayout; import android.widget.spinner; import android.widget.viewswitcher; public class postinfofragment extends fragment { private menuitem myactionmenuitem; private button myactionbutton; private actionbar actionbar; private string post_title,post_desc,post_price,post_quantity,post_category_id; // int post_category_id = 0; private edittext submit_post_title,submit_post_desc, submit_post_total_price,submit_post_total_quantity; private submitpostactivity s; setpostinfo info; list<string> post_details; private linearlayout submit_post_total_quantity_linear; imageview submit_post_img; private imageloader imageloader; private handler handler; private gridview gridgallery; private customgalleryadapter adapter; private viewswitcher viewswitcher; private imageview imgsinglepick; private arraylist<customgallery> datat; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_post_info, container, false); initimageloader(); gridgallery = (gridview)rootview.findviewbyid(r.id.imggridgallery); handler = new handler(); gridgallery.setfastscrollenabled(true); adapter = new customgalleryadapter(getactivity(), imageloader); adapter.setmultiplepick(false); gridgallery.setadapter(adapter); viewswitcher = (viewswitcher) rootview.findviewbyid(r.id.viewswitcher); viewswitcher.setdisplayedchild(1); imgsinglepick = (imageview)rootview. findviewbyid(r.id.imgsinglepick); //s = (submitpostactivity)getactivity(); submit_post_title = (edittext)rootview.findviewbyid(r.id.submit_post_title); submit_post_desc = (edittext)rootview.findviewbyid(r.id.submit_post_description); submit_post_total_price = (edittext)rootview.findviewbyid(r.id.submit_post_total_price); submit_post_total_quantity = (edittext)rootview.findviewbyid(r.id.submit_post_total_quantity); submit_post_total_quantity_linear = (linearlayout)rootview.findviewbyid(r.id.submit_post_total_qty_linear); actionbar = getactivity().getactionbar(); sethasoptionsmenu(true); post_details = new arraylist<string>(); res = getresources(); setlisteners(); homecoming rootview; } private void setlisteners(){ gridgallery.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int postion, long id) { intent = new intent(action.action_multiple_pick); startactivityforresult(i, 200); } }); imgsinglepick.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { intent = new intent(action.action_multiple_pick); startactivityforresult(i, 200); /*for single pick * intent = new intent(action.action_pick); startactivityforresult(i, 100);*/ } }); final typedarray selectedvalues = res.obtaintypedarray(r.array.categoryvalues); submit_post_total_quantity_linear.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { submit_post_total_quantity.requestfocus(); } }); } private void initimageloader() { displayimageoptions defaultoptions = new displayimageoptions.builder() .cacheondisc().imagescaletype(imagescaletype.exactly_stretched) .bitmapconfig(bitmap.config.rgb_565).build(); imageloaderconfiguration.builder builder = new imageloaderconfiguration.builder( getactivity()).defaultdisplayimageoptions(defaultoptions).memorycache( new weakmemorycache()); imageloaderconfiguration config = builder.build(); imageloader = imageloader.getinstance(); imageloader.init(config); } @override public void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == 200 && resultcode == activity.result_ok) { string[] all_path = data.getstringarrayextra("allpath"); datat = new arraylist<customgallery>(); (string string : all_path) { customgallery item = new customgallery(); item.sdcardpath = string; datat.add(item); log.d("selected", string); } viewswitcher.setdisplayedchild(0); adapter.addall(datat); }else if (requestcode == 100 && resultcode == activity.result_ok) { adapter.clear(); viewswitcher.setdisplayedchild(1); string single_path = data.getstringextra("single_path"); imageloader.displayimage("file://" + single_path, imgsinglepick); }else{ super.onactivityresult(requestcode, resultcode, data); } } public interface setpostinfo{ public void pass_post_details(list<string> post_details); } @override public void onattach(activity activity) { super.onattach(activity); // makes sure container activity has implemented // callback interface. if not, throws exception try{ info = (setpostinfo)activity; }catch(classcastexception e){ throw new classcastexception(activity.tostring() + "must implemet setpostinfo"); } } @override public void onpause() { super.onpause(); passforminputs(); } private void passforminputs(){ try{ post_title = submit_post_title.gettext().tostring(); post_desc = submit_post_desc.gettext().tostring(); post_price = submit_post_total_price.gettext().tostring(); post_quantity = submit_post_total_quantity.gettext().tostring(); post_details.add(post_title); post_details.add(post_desc); post_details.add(post_quantity); post_details.add(post_price); post_details.add(post_category_id); log.d("post_info", "hello"+ post_details.get(2) ); log.d("post_info1", "llo"+ post_quantity ); info.pass_post_details(post_details); }catch(exception e){ e.printstacktrace(); } } }
2nd fragment has 3-4 fields , passing info 3rd fragment..
this 3rd fragment-->personalinfofragment
bundle com.jbandroid.fragment; import java.io.file; import java.io.fileinputstream; import java.net.malformedurlexception; import java.net.urisyntaxexception; import java.security.invalidkeyexception; import java.util.arraylist; import java.util.list; import android.app.actionbar; import android.content.context; import android.net.connectivitymanager; import android.net.networkinfo; import android.os.bundle; import android.support.v4.app.fragment; import android.util.log; import android.view.layoutinflater; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.widget.button; import android.widget.edittext; import com.jbandroid.postactivity; import com.jbandroid.r; import com.jbandroid.gallery.customgallery; import com.jbandroid.mobileservice.post; import com.jbandroid.model.toastactivity; import com.microsoft.windowsazure.mobileservices.mobileserviceclient; import com.microsoft.windowsazure.mobileservices.mobileservicetable; import com.microsoft.windowsazure.mobileservices.servicefilterresponse; import com.microsoft.windowsazure.mobileservices.tableoperationcallback; import com.microsoft.azure.storage.*; import com.microsoft.azure.storage.blob.*; public class personalinfofragment extends fragment { private menuitem myactionmenuitem; private button myactionbutton; private actionbar actionbar; list<string> post_details; arraylist<customgallery> selected; private mobileserviceclient mclient; private mobileservicetable<post> post_table; string country,city,area,user_name,user_email,user_phone; post post; private edittext submit_post_user_name; private edittext submit_post_user_email; private edittext submit_post_user_phone; toastactivity toastactivity; private cloudstorageaccount storageaccount; list<string> imagename; cloudblobclient blobclient; cloudblobcontainer blobcontainer; blobcontainerpermissions containerpermissions; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_personal_info,container,false); actionbar = getactivity().getactionbar(); sethasoptionsmenu(true); imagename = new arraylist<string>(); submit_post_user_name = (edittext)rootview.findviewbyid(r.id.submit_post_user_name); submit_post_user_email = (edittext)rootview.findviewbyid(r.id.submit_post_user_email); submit_post_user_phone = (edittext)rootview.findviewbyid(r.id.submit_post_user_phone); toastactivity = new toastactivity(); post = new post(); homecoming rootview; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } public void get_post_details(list<string> post_details){ this.post_details = post_details; // this.selected = selected; log.d("personalfrag(postinfo)", post_details.get(5)); } public void get_post_location_details(string country,string city,string area){ this.country = country; this.city = city; this.area = area; log.d("personal", area); } public void oncreateoptionsmenu(menu menu,menuinflater inflater) { super.oncreateoptionsmenu(menu, inflater); // inflate menu items utilize in action bar inflater.inflate(r.menu.mymenu, menu); // here action view defined myactionmenuitem = menu.finditem(r.id.my_action); view actionview = myactionmenuitem.getactionview(); // button view part of action view if(actionview != null) { myactionbutton = (button) actionview.findviewbyid(r.id.action_btn); myactionbutton.settext(r.string.txt_submit); if(myactionbutton != null) { // set listener called when return/enter key pressed myactionbutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { myactionbutton.setenabled(false); myactionbutton.settext("submitting.."); setpostitems(); insertintodb(); log.d("postinfo",post_details.get(2)); //toastactivity.displaytoast(getactivity(), post_details.get(0)); } }); } } } public void setpostitems(){ user_name = submit_post_user_name.gettext().tostring(); user_email = submit_post_user_email.gettext().tostring(); user_phone = submit_post_user_phone.gettext().tostring(); //this values 1st fragment post.setpost_name(post_details.get(0)); post.setpost_description(post_details.get(1)); post.setpost_totalquantity((integer.valueof(post_details.get(2))).intvalue()); post.setpost_total_price((integer.valueof(post_details.get(3))).intvalue()); post.setcategory_id((integer.valueof(post_details.get(4))).intvalue()); //this values 2nd fragment post.setpost_country(country); post.setpost_city(city); post.setpost_area(area); //this values fragment post.setpost_user_name(user_name); post.setpost_user_email(user_email); post.setpost_user_phone(user_phone); post.setpost_user_id(1); } public void insertintodb(){ if(mclient == null) { return; } if(isonline()){ post_table.insert(post, new tableoperationcallback<post>() { @override public void oncompleted(post post, exception exception, servicefilterresponse response) { if(exception == null){ toastactivity.displaytoast(getactivity(), "post submitted"); }else{ //toastactivity.displaytoast(getactivity(), "error:"+exception.tostring()); toastactivity.displaytoast(getactivity(), "problem connecting server"); log.d("error",exception.tostring(),exception.getcause()); } } }); }else{ toastactivity.displaytoast(getactivity(), "sorry! no net connection"); } myactionbutton.setenabled(true); myactionbutton.settext(r.string.txt_submit); } public boolean isonline() { connectivitymanager cm = (connectivitymanager) getactivity().getsystemservice(context.connectivity_service); networkinfo netinfo = cm.getactivenetworkinfo(); if (netinfo != null && netinfo.isconnectedorconnecting()) { homecoming true; } homecoming false; } }
stacktrace:
10-09 12:14:57.221: d/personal(546): vasai 10-09 12:14:57.231: d/submitarea(546): vasai 10-09 12:14:59.751: d/post_info(546): hello 10-09 12:14:59.751: d/post_info1(546): llo45 10-09 12:14:59.751: d/post_info1(546): +++ log: entry corrupt or truncated 10-09 12:14:59.761: d/submitpostinfo(546): hello 10-09 12:15:08.260: d/androidruntime(546): shutting downwards vm 10-09 12:15:08.260: w/dalvikvm(546): threadid=1: thread exiting uncaught exception (group=0x409961f8) 10-09 12:15:08.300: e/androidruntime(546): fatal exception: main 10-09 12:15:08.300: e/androidruntime(546): java.lang.numberformatexception: invalid int: "" 10-09 12:15:08.300: e/androidruntime(546): @ java.lang.integer.invalidint(integer.java:138) 10-09 12:15:08.300: e/androidruntime(546): @ java.lang.integer.parseint(integer.java:359) 10-09 12:15:08.300: e/androidruntime(546): @ java.lang.integer.parseint(integer.java:332) 10-09 12:15:08.300: e/androidruntime(546): @ java.lang.integer.valueof(integer.java:490) 10-09 12:15:08.300: e/androidruntime(546): @ com.jbandroid.fragment.personalinfofragment.setpostitems(personalinfofragment.java:198) 10-09 12:15:08.300: e/androidruntime(546): @ com.jbandroid.fragment.personalinfofragment$1.onclick(personalinfofragment.java:177) 10-09 12:15:08.300: e/androidruntime(546): @ android.view.view.performclick(view.java:3480) 10-09 12:15:08.300: e/androidruntime(546): @ android.view.view$performclick.run(view.java:13983) 10-09 12:15:08.300: e/androidruntime(546): @ android.os.handler.handlecallback(handler.java:605) 10-09 12:15:08.300: e/androidruntime(546): @ android.os.handler.dispatchmessage(handler.java:92) 10-09 12:15:08.300: e/androidruntime(546): @ android.os.looper.loop(looper.java:137) 10-09 12:15:08.300: e/androidruntime(546): @ android.app.activitythread.main(activitythread.java:4340) 10-09 12:15:08.300: e/androidruntime(546): @ java.lang.reflect.method.invokenative(native method) 10-09 12:15:08.300: e/androidruntime(546): @ java.lang.reflect.method.invoke(method.java:511) 10-09 12:15:08.300: e/androidruntime(546): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:784) 10-09 12:15:08.300: e/androidruntime(546): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:551) 10-09 12:15:08.300: e/androidruntime(546): @ dalvik.system.nativestart.main(native method) 10-09 12:15:11.705: d/gralloc_goldfish(576): emulator without gpu emulation detected.
also can see in stacktrace..
log.d("post_info", "hello"+ post_details.get(2) ); log.d("post_info1", "llo"+ post_quantity );
post_quantity value beingness printed while post_details.get(2) empty..cant understand behaviour.
update: found issue(it improper exception handling).. when going custom gallery activity 1st fragment.. onpause called.. , here not handling exception since list beingness filled empty data..as add together functions beingness called , exception thrown..i.e. exception thrown in line
info.pass_post_details(post_details);
in passforminputs() method.. code before line anyhow executed..now next time when onpause called again.. real values added in list below empty values. had total 10 values in mmy list , when tried print 11th value got 1st value printed..
as stacktrace suggest. it's numberformatexception, invalid int ""
. in personalinfofragment
the problem of code never checked beingness returned calling post_items.get(2);
before calling integer.valueof()
.
my suggestion check returned post_items.get(2)
before calling integer.valueof()
, wrap integer.valueof
in try-catch block
string quantitystr = post_details.get(2); if(quantitystr != null && quantitystr.length() > 0) { try{ int qty = integer.valueof(quantitystr); //do logic. }catch(numberformatexception e) { //unable parse, not valid int } }
android android-fragments nullpointerexception
Comments
Post a Comment