java - Setting text to multiple TextFields with Asynctasks crashes app -
what want achieve following: in view, have 4 textviews should show amount of items in connected databases. done using simple php script.
the problem have correct values database, when try set values textviews, app crashes.
i have tried if using many asynctasks problem, when decide comment out line inside settextfortextview() function, works charm , expected values printed log. being said, dont think using multiple asynctasks @ same time problem here, have no idea is.
something might worth mentioning when comment out following bit this, app doesn't crash...
shoesamount = (textview) findviewbyid(r.id.menutvshoesamount); databasetask taska = new databasetask(shoesamount); taska.execute("getamount", "shoes"); // tshirtsamounts = (textview) findviewbyid(r.id.menutvtshirtsamount); // databasetask taskb = new databasetask(tshirtsamounts); // taskb.execute("getamount", "tshirts"); // // jeansamount = (textview) findviewbyid(r.id.menutvjeansamount); // databasetask taskc = new databasetask(jeansamount); // taskc.execute("getamount", "jeans"); // // blousesamount = (textview) findviewbyid(r.id.menutvblousesamount); // databasetask taskd = new databasetask(blousesamount); // taskd.execute("getamount", "blouses");
but again other combination example one, , app crashes again...
// shoesamount = (textview) findviewbyid(r.id.menutvshoesamount); // databasetask taska = new databasetask(shoesamount); // taska.execute("getamount", "shoes"); // tshirtsamounts = (textview) findviewbyid(r.id.menutvtshirtsamount); databasetask taskb = new databasetask(tshirtsamounts); taskb.execute("getamount", "tshirts"); // // jeansamount = (textview) findviewbyid(r.id.menutvjeansamount); // databasetask taskc = new databasetask(jeansamount); // taskc.execute("getamount", "jeans"); // // blousesamount = (textview) findviewbyid(r.id.menutvblousesamount); // databasetask taskd = new databasetask(blousesamount); // taskd.execute("getamount", "blouses");
does can point me out direction might going wrong?
thanks! (here full code)
package ishopper.theindiestudio.com.appname; import android.app.alertdialog; import android.content.context; import android.content.intent; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.os.asynctask; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.util.log; import android.view.view; import android.widget.arrayadapter; import android.widget.button; import android.widget.horizontalscrollview; import android.widget.imagebutton; import android.widget.imageview; import android.widget.spinner; import android.widget.textview; import java.io.bufferedreader; import java.io.bufferedwriter; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.outputstream; import java.io.outputstreamwriter; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; import java.net.urlencoder; import java.util.arraylist; public class menu extends appcompatactivity { private imagebutton btnhelp, btnaccount, newest1, newest2, newest3; private textview shoesamount, tshirtsamounts, jeansamount, blousesamount; private button browseshoes, browsetshirts, browsejeans, browseblouses; private context context; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_menu); context = this; btnhelp = (imagebutton) findviewbyid(r.id.menubtnhelp); btnaccount = (imagebutton) findviewbyid(r.id.menubtnaccount); btnaccount.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { onaccountoptions(); } }); btnhelp.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { onhelp(); } }); newest1 = (imagebutton) findviewbyid(r.id.menuibnewest1); newest2 = (imagebutton) findviewbyid(r.id.menuibnewest2); newest3 = (imagebutton) findviewbyid(r.id.menuibnewest3); //set newest items shoesamount = (textview) findviewbyid(r.id.menutvshoesamount); databasetask taska = new databasetask(shoesamount); taska.execute("getamount", "shoes"); tshirtsamounts = (textview) findviewbyid(r.id.menutvtshirtsamount); databasetask taskb = new databasetask(tshirtsamounts); taskb.execute("getamount", "tshirts"); jeansamount = (textview) findviewbyid(r.id.menutvjeansamount); databasetask taskc = new databasetask(jeansamount); taskc.execute("getamount", "jeans"); blousesamount = (textview) findviewbyid(r.id.menutvblousesamount); databasetask taskd = new databasetask(blousesamount); taskd.execute("getamount", "blouses"); } public void settextfortextview(textview textview, string result){ textview.settext(result); } public void onaccountoptions () { intent = new intent(this, accountdetailsactivity.class); startactivity(i); } public void onhelp () { } public void onbackpressed() { //disabled button } private class databasetask extends asynctask<string, void, string> { private string tasktype; private string producttype; private textview textview; private imagebutton imagebutton; databasetask (textview mtextview){textview = mtextview;} @override protected string doinbackground(string... params) { string task = params[0]; if (task.equals("getamount")) { log.e("doinbg", "getamount"); string login_url = "http://url.eu/directory/script.php"; string tproducttype = params[1]; try { url url = new url(login_url); httpurlconnection httpurlconnection = (httpurlconnection) url.openconnection(); httpurlconnection.setrequestmethod("post"); httpurlconnection.setdooutput(true); httpurlconnection.setdoinput(true); outputstream outputstream = httpurlconnection.getoutputstream(); bufferedwriter bufferedwriter = new bufferedwriter(new outputstreamwriter(outputstream, "utf-8")); string postdata = urlencoder.encode("producttable", "utf-8") + "=" + urlencoder.encode(tproducttype, "utf-8"); bufferedwriter.write(postdata); bufferedwriter.flush(); bufferedwriter.close(); outputstream.close(); inputstream inputstream = httpurlconnection.getinputstream(); bufferedreader bufferedreader = new bufferedreader(new inputstreamreader(inputstream, "iso-8859-1")); string result = ""; string line = ""; while ((line = bufferedreader.readline()) != null) { result += line; log.e("doinbgresult", result.tostring()); } bufferedreader.close(); inputstream.close(); httpurlconnection.disconnect(); menu activity = (menu) context; activity.settextfortextview(textview, result.tostring()); } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } return null; } } }
the asynctask
's doinbackground()
runs in thread , can't change ui thread, main one. suggest same job in onpostexecute()
after finish long job. return string result
want use main thread
bufferedreader.close(); inputstream.close(); httpurlconnection.disconnect(); // no need //menu activity = (menu) context; //activity.settextfortextview(textview, result.tostring()); reutrn result;
then update text
@override protected void onpostexecute(string result) { super.onpostexecute(result); if(result != null){ settextfortextview(textview, result); } }
Comments
Post a Comment