android - Hide a EditText & make it visible in AsyncTask -
i making android app send id_user , id_status remote database asynctask. if success == 1, edittext , button visible automatically.
commentactivity
package com.portalalumni; import java.util.arraylist; import java.util.hashmap; import java.util.list; import org.apache.http.namevaluepair; import org.apache.http.message.basicnamevaluepair; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import android.os.asynctask; import android.os.bundle; import android.os.handler; import android.app.activity; import android.app.progressdialog; import android.content.intent; import android.util.log; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.adapterview; import android.widget.button; import android.widget.edittext; import android.widget.listview; import android.widget.textview; import android.widget.toast; public class commentactivity extends activity { sessionmanager session; boolean isvisible; // progress dialog private progressdialog pdialog; // creating json parser object jsonparser jparser = new jsonparser(); // url private static string url_cek_pendidikan = "http://192.168.43.13/alumni/cek_pendidikan.php"; // json node names public static final string tag_success = "success"; string id_user,email,id_status; private edittext comment; button btnaddcomment; private final handler handler = new handler(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_comment); session = new sessionmanager(getapplicationcontext()); toast.maketext(getapplicationcontext(), "user login status: " + session.isloggedin(), toast.length_long) .show(); session.checklogin(); //hashmap login hashmap<string, string> user = session.getuserdetails(); id_user = user.get(sessionmanager.key_iduser); bundle extras = getintent().getextras(); if(extras!=null){ id_status = extras.getstring("id_status"); } // loading status , comment in background thread new loadcekpenddkactivity().execute(); comment = (edittext) findviewbyid(r.id.comment); btnaddcomment = (button) findviewbyid(r.id.dm_send); // button click event btnaddcomment.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { // creating comment in background thread new loadupdateactivity().execute(); //comment.gettext().clear(); } }); // show button in action bar. getactionbar().setdisplayhomeasupenabled(true); } public void set_visibility_visible(){ this.isvisible = true; } class loadcekpenddkactivity extends asynctask<string, string, string> { /** * before starting background thread show progress dialog * */ @override protected void onpreexecute() { super.onpreexecute(); /*pdialog = new progressdialog(commentactivity.this); pdialog.setmessage("loading.."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show();*/ } /** * creating product * */ protected string doinbackground(string... args) { // building parameters list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("id_user", id_user)); params.add(new basicnamevaluepair("id_status", id_status)); // getting json object // note create product url accepts post method jsonobject json = jparser.makehttprequest(url_cek_pendidikan,"get", params); // check log cat fro response log.d("update", json.tostring()); // check success tag try { int success = json.getint(tag_success); if (success == 1) { comment.setvisibility(view.visible); btnaddcomment.setvisibility(view.visible); } else { comment.setvisibility(view.gone); btnaddcomment.setvisibility(view.gone); } } catch (jsonexception e) { e.printstacktrace(); } return null; } /** * after completing background task dismiss progress dialog * **/ protected void onpostexecute(string file_url) { // dismiss dialog once done //pdialog.dismiss(); } } }
after adding timer task , timer run code. app crashed , got following logcat error:
04-23 22:15:34.359: e/androidruntime(1050): fatal exception: asynctask #4 04-23 22:15:34.359: e/androidruntime(1050): java.lang.runtimeexception: error occured while executing doinbackground() 04-23 22:15:34.359: e/androidruntime(1050): @ android.os.asynctask$3.done(asynctask.java:278) 04-23 22:15:34.359: e/androidruntime(1050): @ java.util.concurrent.futuretask$sync.innersetexception(futuretask.java:273) 04-23 22:15:34.359: e/androidruntime(1050): @ java.util.concurrent.futuretask.setexception(futuretask.java:124) 04-23 22:15:34.359: e/androidruntime(1050): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:307) 04-23 22:15:34.359: e/androidruntime(1050): @ java.util.concurrent.futuretask.run(futuretask.java:137) 04-23 22:15:34.359: e/androidruntime(1050): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:208) 04-23 22:15:34.359: e/androidruntime(1050): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1076) 04-23 22:15:34.359: e/androidruntime(1050): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:569) 04-23 22:15:34.359: e/androidruntime(1050): @ java.lang.thread.run(thread.java:856) 04-23 22:15:34.359: e/androidruntime(1050): caused by: android.view.viewrootimpl$calledfromwrongthreadexception: original thread created view hierarchy can touch views. 04-23 22:15:34.359: e/androidruntime(1050): @ android.view.viewrootimpl.checkthread(viewrootimpl.java:3939) 04-23 22:15:34.359: e/androidruntime(1050): @ android.view.viewrootimpl.requestlayout(viewrootimpl.java:701) 04-23 22:15:34.359: e/androidruntime(1050): @ android.view.view.requestlayout(view.java:12555) 04-23 22:15:34.359: e/androidruntime(1050): @ android.view.view.requestlayout(view.java:12555) 04-23 22:15:34.359: e/androidruntime(1050): @ android.view.view.requestlayout(view.java:12555) 04-23 22:15:34.359: e/androidruntime(1050): @ android.view.view.requestlayout(view.java:12555) 04-23 22:15:34.359: e/androidruntime(1050): @ android.support.v4.widget.drawerlayout.requestlayout(drawerlayout.java:762) 04-23 22:15:34.359: e/androidruntime(1050): @ android.view.view.requestlayout(view.java:12555) 04-23 22:15:34.359: e/androidruntime(1050): @ android.widget.relativelayout.requestlayout(relativelayout.java:268) 04-23 22:15:34.359: e/androidruntime(1050): @ android.view.view.requestlayout(view.java:12555) 04-23 22:15:34.359: e/androidruntime(1050): @ android.view.view.setflags(view.java:6659) 04-23 22:15:34.359: e/androidruntime(1050): @ android.view.view.setvisibility(view.java:4564) 04-23 22:15:34.359: e/androidruntime(1050): @ com.portalalumni.commentactivity$loadcekpenddkactivity.doinbackground(commentactivity.java:521) 04-23 22:15:34.359: e/androidruntime(1050): @ com.portalalumni.commentactivity$loadcekpenddkactivity.doinbackground(commentactivity.java:1) 04-23 22:15:34.359: e/androidruntime(1050): @ android.os.asynctask$2.call(asynctask.java:264) 04-23 22:15:34.359: e/androidruntime(1050): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:305) 04-23 22:15:34.359: e/androidruntime(1050): ... 5 more 04-23 22:15:35.999: e/windowmanager(1050): activity com.portalalumni.commentactivity has leaked window com.android.internal.policy.impl.phonewindow$decorview@41439380 added here 04-23 22:15:35.999: e/windowmanager(1050): android.view.windowleaked: activity com.portalalumni.commentactivity has leaked window com.android.internal.policy.impl.phonewindow$decorview@41439380 added here 04-23 22:15:35.999: e/windowmanager(1050): @ android.view.viewrootimpl.<init>(viewrootimpl.java:343) 04-23 22:15:35.999: e/windowmanager(1050): @ android.view.windowmanagerimpl.addview(windowmanagerimpl.java:245) 04-23 22:15:35.999: e/windowmanager(1050): @ android.view.windowmanagerimpl.addview(windowmanagerimpl.java:193) 04-23 22:15:35.999: e/windowmanager(1050): @ android.view.windowmanagerimpl$compatmodewrapper.addview(windowmanagerimpl.java:118) 04-23 22:15:35.999: e/windowmanager(1050): @ android.view.window$localwindowmanager.addview(window.java:537) 04-23 22:15:35.999: e/windowmanager(1050): @ android.app.dialog.show(dialog.java:274) 04-23 22:15:35.999: e/windowmanager(1050): @ com.portalalumni.commentactivity$loadcekpenddkactivity.onpreexecute(commentactivity.java:497) 04-23 22:15:35.999: e/windowmanager(1050): @ android.os.asynctask.executeonexecutor(asynctask.java:561) 04-23 22:15:35.999: e/windowmanager(1050): @ android.os.asynctask.execute(asynctask.java:511) 04-23 22:15:35.999: e/windowmanager(1050): @ com.portalalumni.commentactivity.oncreate(commentactivity.java:105) 04-23 22:15:35.999: e/windowmanager(1050): @ android.app.activity.performcreate(activity.java:4465) 04-23 22:15:35.999: e/windowmanager(1050): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1049) 04-23 22:15:35.999: e/windowmanager(1050): @ android.app.activitythread.performlaunchactivity(activitythread.java:1919) 04-23 22:15:35.999: e/windowmanager(1050): @ android.app.activitythread.handlelaunchactivity(activitythread.java:1980) 04-23 22:15:35.999: e/windowmanager(1050): @ android.app.activitythread.access$600(activitythread.java:122) 04-23 22:15:35.999: e/windowmanager(1050): @ android.app.activitythread$h.handlemessage(activitythread.java:1146) 04-23 22:15:35.999: e/windowmanager(1050): @ android.os.handler.dispatchmessage(handler.java:99) 04-23 22:15:35.999: e/windowmanager(1050): @ android.os.looper.loop(looper.java:137) 04-23 22:15:35.999: e/windowmanager(1050): @ android.app.activitythread.main(activitythread.java:4340) 04-23 22:15:35.999: e/windowmanager(1050): @ java.lang.reflect.method.invokenative(native method) 04-23 22:15:35.999: e/windowmanager(1050): @ java.lang.reflect.method.invoke(method.java:511) 04-23 22:15:35.999: e/windowmanager(1050): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:784) 04-23 22:15:35.999: e/windowmanager(1050): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:551) 04-23 22:15:35.999: e/windowmanager(1050): @ dalvik.system.nativestart.main(native method) 04-23 22:15:36.149: e/windowmanager(1050): activity com.portalalumni.commentactivity has leaked window com.android.internal.policy.impl.phonewindow$decorview@4145bd68 added here 04-23 22:15:36.149: e/windowmanager(1050): android.view.windowleaked: activity com.portalalumni.commentactivity has leaked window com.android.internal.policy.impl.phonewindow$decorview@4145bd68 added here 04-23 22:15:36.149: e/windowmanager(1050): @ android.view.viewrootimpl.<init>(viewrootimpl.java:343) 04-23 22:15:36.149: e/windowmanager(1050): @ android.view.windowmanagerimpl.addview(windowmanagerimpl.java:245) 04-23 22:15:36.149: e/windowmanager(1050): @ android.view.windowmanagerimpl.addview(windowmanagerimpl.java:193) 04-23 22:15:36.149: e/windowmanager(1050): @ android.view.windowmanagerimpl$compatmodewrapper.addview(windowmanagerimpl.java:118) 04-23 22:15:36.149: e/windowmanager(1050): @ android.view.window$localwindowmanager.addview(window.java:537) 04-23 22:15:36.149: e/windowmanager(1050): @ android.app.dialog.show(dialog.java:274) 04-23 22:15:36.149: e/windowmanager(1050): @ com.portalalumni.commentactivity$loadstatusactivity.onpreexecute(commentactivity.java:228) 04-23 22:15:36.149: e/windowmanager(1050): @ android.os.asynctask.executeonexecutor(asynctask.java:561) 04-23 22:15:36.149: e/windowmanager(1050): @ android.os.asynctask.execute(asynctask.java:511) 04-23 22:15:36.149: e/windowmanager(1050): @ com.portalalumni.commentactivity.oncreate(commentactivity.java:106) 04-23 22:15:36.149: e/windowmanager(1050): @ android.app.activity.performcreate(activity.java:4465) 04-23 22:15:36.149: e/windowmanager(1050): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1049) 04-23 22:15:36.149: e/windowmanager(1050): @ android.app.activitythread.performlaunchactivity(activitythread.java:1919) 04-23 22:15:36.149: e/windowmanager(1050): @ android.app.activitythread.handlelaunchactivity(activitythread.java:1980) 04-23 22:15:36.149: e/windowmanager(1050): @ android.app.activitythread.access$600(activitythread.java:122) 04-23 22:15:36.149: e/windowmanager(1050): @ android.app.activitythread$h.handlemessage(activitythread.java:1146) 04-23 22:15:36.149: e/windowmanager(1050): @ android.os.handler.dispatchmessage(handler.java:99) 04-23 22:15:36.149: e/windowmanager(1050): @ android.os.looper.loop(looper.java:137) 04-23 22:15:36.149: e/windowmanager(1050): @ android.app.activitythread.main(activitythread.java:4340) 04-23 22:15:36.149: e/windowmanager(1050): @ java.lang.reflect.method.invokenative(native method) 04-23 22:15:36.149: e/windowmanager(1050): @ java.lang.reflect.method.invoke(method.java:511) 04-23 22:15:36.149: e/windowmanager(1050): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:784) 04-23 22:15:36.149: e/windowmanager(1050): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:551) 04-23 22:15:36.149: e/windowmanager(1050): @ dalvik.system.nativestart.main(native method)
doinbackground()
runs in separate thread can't touch view
s of main activity
inside it, that's why you're seeing calledfromwrongthreadexception
. instead, can call in any method of asynctask
doinbackground()
.
as seems you're testing result of something, recommend setting in onpostexecute()
method next called once out of doinbackground()
method.
Comments
Post a Comment