android - substring in AsyncTask cause app to force quit -
i have code:
package com.jamie.translate; import android.content.context; import android.net.uri; import android.os.asynctask; import android.util.log; import android.widget.edittext; import android.widget.textview; import org.apache.http.httpresponse; import org.apache.http.client.httpclient; import org.apache.http.client.methods.httpget; import org.apache.http.impl.client.defaulthttpclient; import java.io.bufferedreader; import java.net.uri; /** * created jamie on 25/6/2015. */ public class translatetexttask extends asynctask<textview, void, string> { textview t; string result = ""; string sourcetext = ""; string sourcelanguage = ""; string resultlanguage = ""; public translatetexttask(string _sourcetext, string _sourcelanguage, string _resultlanguage) { this.sourcetext = _sourcetext; this.sourcelanguage = _sourcelanguage; this.resultlanguage = _resultlanguage; } protected void d(string tag, string errormessage) { mainactivity.shownormalmessage(mainactivity.applicationcontext, errormessage); } @override protected string doinbackground(textview... params) { this.t = params[0]; return gettranslatedtext(); } final string gettranslatedtext() { uri.builder builder = new uri.builder(); string langpair; uri uri = null; final httpclient httpclient; final httpget request; final httpresponse[] response = new httpresponse[1]; final bufferedreader reader = null; final string jsonresult = ""; string translatedstring; if(sourcetext.length() > 500) { mainactivity.showerrormessage(mainactivity.applicationcontext, "source text can max. 500 characters"); return ""; } d("test", "tag2");try { string x = ("en - english").substring(0, 2); string y = resultlanguage.substring(0, 2); langpair = sourcelanguage.substring(0, 2) + "|" + resultlanguage.substring(0, 2); } catch (exception e) { mainactivity.showerrormessage(mainactivity.applicationcontext, "source language or result language invalid."); return ""; } d("test", "tag3");try { uri = new uri(builder.scheme("http") .authority("api.mymemory.translated.net") .appendpath("get") .appendqueryparameter("langpair", langpair) .appendqueryparameter("q", sourcetext) .build() .tostring()); } catch (exception e) { mainactivity.showerrormessage(mainactivity.applicationcontext, "failed build http request url."); return ""; } d("test", "tag4");try { httpclient = new defaulthttpclient(); request = new httpget(); request.seturi(uri); } catch (exception e) { mainactivity.showerrormessage(mainactivity.applicationcontext, "failed build http request."); return ""; } d("test", "tag5");try { // response = httpclient.execute(request); d("test", "tag6");try { response[0] = (httpclient.execute(request)); return mainactivity.handletranslaterequest(mainactivity.applicationcontext, response[0]); } catch (exception e) { mainactivity.showerrormessage(mainactivity.applicationcontext, e.tostring()); return ""; } } catch (exception e) { mainactivity.showerrormessage(mainactivity.applicationcontext, e.tostring()); // mainactivity.showerrormessage(mainactivity.applicationcontext, "failed complete http request."); return ""; } } protected void onpostexecute(string result) { t.settext(result); } }
and start code by:
new translatetexttask(((edittext) findviewbyid(r.id.source)).gettext(), translatesourcelanguage, translateresultlanguage).execute((edittext) findviewbyid(r.id.result));
example values of values of variable in debug process include
((edittext) findviewbyid(r.id.source)).gettext(): "hi" translatesourcelanguage: "en - english" translateresultlanguage: "it - italiano"
there no internet connection problem. during debug process, set enough breakpoints , found force quit problem happened on line:
string x = ("en - english").substring(0, 2);
what can solve problem? seems super-simple problem can't stop @ all.
edit
logcat message:
06-25 18:41:04.318 9337-9534/com.jamie.translate e/androidruntime﹕ fatal exception: asynctask #1 process: com.jamie.translate, pid: 9337 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:200) @ android.os.handler.<init>(handler.java:114) @ android.widget.toast$tn.<init>(toast.java:376) @ android.widget.toast.<init>(toast.java:108) @ android.widget.toast.maketext(toast.java:267) @ com.jamie.translate.mainactivity.shownormalmessage(mainactivity.java:26) @ com.jamie.translate.translatetexttask.d(translatetexttask.java:35) @ com.jamie.translate.translatetexttask.gettranslatedtext(translatetexttask.java:60) @ com.jamie.translate.translatetexttask.doinbackground(translatetexttask.java:41) @ com.jamie.translate.translatetexttask.doinbackground(translatetexttask.java:21) @ android.os.asynctask$2.call(asynctask.java:288) @ java.util.concurrent.futuretask.run(futuretask.java:237) at android.os.asynctask$serialexecutor$1.run(asynctask.java:231) at java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) at java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) at java.lang.thread.run(thread.java:841)
you must not touch ui (i.e. posting toasts or update textview etc) on doinbackground()
. if need update ui need either in onpostexecute()
or use onprogressupdate()
if fit.
Comments
Post a Comment