java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() in Android -
i error in logcat when press button connects json mysql database data this. error:
05-15 12:09:39.569 4982-5178/? e/androidruntime﹕ fatal exception: asynctask #1 process: com.example.ospitznik.enarrations, pid: 4982 java.lang.runtimeexception: error occured while executing doinbackground() @ android.os.asynctask$3.done(asynctask.java:300) @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:355) @ java.util.concurrent.futuretask.setexception(futuretask.java:222) @ java.util.concurrent.futuretask.run(futuretask.java:242) @ android.os.asynctask$serialexecutor$1.run(asynctask.java:231) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) @ java.lang.thread.run(thread.java:841) caused by: java.lang.runtimeexception: can't create handler inside thread has not called looper.prepare() @ android.os.handler.<init>(handler.java:208) @ android.os.handler.<init>(handler.java:122) @ android.app.activity.<init>(activity.java:765) @ com.example.ospitznik.enarrations.narracionesactivity.<init>(narracionesactivity.java:7) @ com.example.ospitznik.enarrations.visoractivity.filtrardatos(visoractivity.java:180) @ com.example.ospitznik.enarrations.visoractivity.access$400(visoractivity.java:30) @ com.example.ospitznik.enarrations.visoractivity$mostrar.doinbackground(visoractivity.java:217) @ com.example.ospitznik.enarrations.visoractivity$mostrar.doinbackground(visoractivity.java:213) @ android.os.asynctask$2.call(asynctask.java:288) @ java.util.concurrent.futuretask.run(futuretask.java:237)
my activity this:
public class visoractivity extends activity { private edittext etnarracion; private textview tvusu; private imageview mostrar; private imageview mas; private imageview menos; private imageview ivbotoninicio; private int posicion=0; private list<narracionesactivity> listanarraciones; private narracionesactivity narraciones; @suppresslint("inlinedapi") @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.activity_visor); tvusu = (textview) findviewbyid(r.id.etusu); etnarracion = (edittext) findviewbyid(r.id.etnarracion); ivbotoninicio = (imageview) findviewbyid(r.id.ivbotoninicio); bundle extras = getintent().getextras(); string usuario = extras.getstring("usuario"); tvusu.settext(usuario); listanarraciones=new arraylist<narracionesactivity>(); ivbotoninicio.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { bundle extras = getintent().getextras(); string usuario = extras.getstring("usuario"); intent = new intent(visoractivity.this, narracionactivity.class); i.putextra("usuario", usuario); startactivityforresult(i, 0); finish(); } }); //mostramos los datos de la persona por pantalla. mostrar=(imageview)findviewbyid(r.id.ivbotonmostrar); mostrar.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { new mostrar ().execute(); } }); mas = (imageview) findviewbyid(r.id.ivbotonder); mas.setonclicklistener(new view.onclicklistener(){ @override public void onclick(view v) { // todo auto-generated method stub if(!listanarraciones.isempty()){ if(posicion>=listanarraciones.size()-1){ posicion=listanarraciones.size()-1; mostrarnarracion(posicion); }else{ posicion++; mostrarnarracion(posicion); } } } }); //se mueve por nuestro arraylist mostrando el objeto anterior menos=(imageview)findviewbyid(r.id.ivbotonizq); menos.setonclicklistener(new view.onclicklistener(){ @override public void onclick(view v) { // todo auto-generated method stub if(!listanarraciones.isempty()){ if(posicion<=0){ posicion=0; mostrarnarracion(posicion); } else{ posicion--; mostrarnarracion(posicion); } } } }); } private string mostrar(){ httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://www.enarrations.com/selectalljson.php"); string resultado=""; httpresponse response; try { response = httpclient.execute(httppost); httpentity entity = response.getentity(); inputstream instream = entity.getcontent(); resultado= convertstreamtostring(instream); } catch (clientprotocolexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } return resultado; } private string convertstreamtostring(inputstream is) throws ioexception { if (is != null) { stringbuilder sb = new stringbuilder(); string line; try { bufferedreader reader = new bufferedreader( new inputstreamreader(is, "utf-8")); while ((line = reader.readline()) != null) { sb.append(line).append("\n"); } } { is.close(); } return sb.tostring(); } else { return ""; } } private boolean filtrardatos(){ listanarraciones.clear(); string data=mostrar(); if(!data.equalsignorecase("")){ jsonobject json; try { json = new jsonobject(data); jsonarray jsonarray = json.optjsonarray("narraciones"); (int = 0; < jsonarray.length(); i++) { narraciones=new narracionesactivity(); jsonobject jsonarraychild = jsonarray.getjsonobject(i); narraciones.setus_cod(jsonarraychild.optstring("us_cod")); narraciones.setus_email(jsonarraychild.optstring("us_email")); narraciones.setus_narracion(jsonarraychild.optstring("us_narracion")); listanarraciones.add(narraciones); } } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } return true; } return false; } private void mostrarnarracion(final int posicion){ runonuithread(new runnable() { @override public void run() { // todo auto-generated method stub narracionesactivity narraciones = listanarraciones.get(posicion); etnarracion.settext(narraciones.getus_narracion()); } }); } class mostrar extends asynctask<string,string,string> { @override protected string doinbackground(string... params) { // todo auto-generated method stub if(filtrardatos())mostrarnarracion(posicion); return null; } } }
because using asynctask
getting data web-service in doinbackground
method no need use runonuithread
updating text in textview use onpostexecute
updating data in view's:
@override protected void onpostexecute(string result) { // todo auto-generated method stub super.onpostexecute(result); if(result !=null) etnarracion.settext(result); }
and change doinbackground
method :
@override protected string doinbackground(string... params) { // todo auto-generated method stub if(filtrardatos()){ return listanarraciones.get(posicion).getus_narracion(); }else return null; }
Comments
Post a Comment