android - java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare(); -
i have android app running thread. want toast message show message.
when this, below exception:
logcat trace:
fatal exception: timer-0 java.lang.runtimeexception: can't create handler inside thread has not called looper.prepare() @ android.os.handler.<init>(handler.java:121) @ android.widget.toast$tn.<init>(toast.java:322) @ android.widget.toast.<init>(toast.java:91) @ android.widget.toast.maketext(toast.java:238)
is there work around pushing toast messages threads user interface?
i got exception because trying make toast popup background thread.
toast needs activity push user interface , threads don't have that.
1 workaround give thread link parent activity , toast that.
put code in thread want send toast message:
parent.runonuithread(new runnable() { public void run() { toast.maketext(parent.getbasecontext(), "hello", toast.length_long).show(); } });
keep link parent activity in background thread created thread. use parent variable in thread class:
private static youractivity parent;
when create thread, pass parent activity parameter through constructor this:
public yourbackgroundthread(youractivity parent) { this.parent = parent; }
now background thread can push toast messages screen.
Comments
Post a Comment