Android thread Update UI error: Can ' t create handler inside thread that have not called looper.prepare ()

Source: Internet
Author: User

There is a button in the mainactivity that binds the Save method

 Public void Save (View view) {        = titletext.gettext (). toString ();         = Lengthtext.gettext (). toString ();         = Executors.newcachedthreadpool ();        Exec.execute (new  Newsservice (Getapplicationcontext (), title,timelength));    

Newsservice Code:

@Override Public voidrun () {String path= "Http://192.168.0.102:8080/videonews/ManageServlet"; Map<String,String> params =NewHashmap<string,string>(); Params.put ("Title", title); Params.put ("Timelength", timelength); Booleanresult=false; Try{result=sendgetrequest (Path,params); } Catch(Exception e) {e.printstacktrace (); }        if(Result) {Toast.maketext (context, r.string.success, Toast.length_long). Show (); } Else{toast.maketext (Context,r.string.error,toast.length_long). Show (); }    }

Error:

04-18 13:06:36.191 2284-2305/test.example.com.newsmanage e/androidruntime:fatal exception:pool-1-thread-1Process:test.example.com.newsmanage, PID:2284Java.lang.RuntimeException:Can' t create handler inside thread that have not called looper.prepare () at android.os.handler.<init> (handler . java:200) at Android.os.Handler.<init> (handler.java:114) at Android.widget.toast$tn.<init> (toast.java:336) at Android.widget.Toast.<init> (toast.java:100) at Android.widget.Toast.makeText (Toast.java:250) at Android.widget.Toast.makeText (Toast.java:277) at Test.example.com.service.NewsService.run (Newsservice.java:86) at Java.util.concurrent.ThreadPoolExecutor.runWorker (Threadpoolexecutor.java:1112) at Java.util.concurrent.threadpoolexecutor$worker.run (Threadpoolexecutor.java:48V) at Java.lang.Thread.run (Thread.java:818)

Checked the data, said that Android can not be in the child thread to refresh the UI. If you want to implement this function. The recommendation is to add hander to your child thread to send a message to update the thread.

This is OK.

1. Add the following code to the activity

PrivateHandler MyHandler =NewHandler () {@Override Public voidhandlemessage (Message msg) {Switch(msg.what) { Case0x00: Toast.maketext (Getapplicationcontext (),"Save Success", Toast.length_short). Show ();  Break; default: Toast.maketext (Getapplicationcontext (),"Save Fail", Toast.length_short). Show (); }        }    };

2. When the thread is started, handler is passed in:

Exec.execute (new Newsservice (Getapplicationcontext (), myhandler,title,timelength));

3. In the thread, send a message to handler:

@Override Public voidrun () {String path= "Http://192.168.0.102:8080/videonews/ManageServlet"; Map<String,String> params =NewHashmap<string,string>(); Params.put ("Title", title); Params.put ("Timelength", timelength); Booleanresult=false; Try{result=sendgetrequest (Path,params); } Catch(Exception e) {e.printstacktrace (); } Message msg=NewMessage (); if(Result) msg.what= 0x00; ElseMsg.what= 0x01;    Handler.sendmessage (msg); }

Complete.

Android thread Update UI error: Can ' t create handler inside thread that have not called looper.prepare ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.