Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

hi i have a problem with my code..my code is

progressD = ProgressDialog.show(MenuUtama.this, "", "Uploading files to server.....", false);
Thread thread = new Thread(new Runnable(){
       public void run(){
           //doFileUpload();
           try {
            // setiap parameter yang akan dikirim melalui http
            // harus encode agar
            // dapat terbaca dengan baik oleh server
            Cursor c = helper.getAll1(almagId);
            Cursor cr = helper.getUpImage(almagId);
                if(c.moveToFirst()){
                    //progressD = ProgressDialog.show(context, title, message)
                        do{
                            String kdstore = URLEncoder.encode(helper.getKdStore(c).toString(), "utf-8");
                            String nama = URLEncoder.encode(helper.getNama(c).toString(), "utf-8");
                            String alamat = URLEncoder.encode(helper.getAlamat(c).toString(), "utf-8");
                            String kdpos = URLEncoder.encode(helper.getKdPos(c).toString(), "utf-8");
                            String notelp = URLEncoder.encode(helper.getNotel(c).toString(), "utf-8");
                            String lng = URLEncoder.encode(helper.getlng(c).toString(), "utf-8");
                            String lat = URLEncoder.encode(helper.getLat(c).toString(), "utf-8");
                            String perush = URLEncoder.encode(helper.getPerus(c).toString(), "utf-8");
                            //String gambar = URLEncoder.encode(helper.getGamb(c).toString(), "utf-8");

                            //Toast.makeText(this, kdstore, Toast.LENGTH_LONG).show();
                            //System.out.println(gambar);
                            url += "?kode_toko=" + kdstore + "&&nama=" + nama + "&&alamat=" + alamat + 
                            "&&kode_pos=" + kdpos + "&&no_telp=" + notelp + "&&longitude=" + lng + "&&latitude=" + lat +
                            "&&perusahaan=" + perush;
                            getRequest(url);
                            url = "http://10.234.165.232/upload_get.php";
                        }while(c.moveToNext());
            }
            if(cr.moveToFirst()){
                do{
                    String kdstore = URLEncoder.encode(helper.getKdstore1(cr), "utf-8");
                    String gambar = URLEncoder.encode(helper.getGam1(cr), "utf-8");
                    url1 += "?kode_toko1=" + kdstore + "&&gambar1=" + gambar;
                    getRequest1(url1);
                    url1 = "http://10.234.165.232/upload_get2.php";
                }while(cr.moveToNext());
            }

            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           MenuUtama.this.runOnUiThread(new Runnable(){
               public void run() {
                   if(progressD.isShowing())
                       progressD.dismiss();
               }
           });
       }
});
thread.start();
return(true);

and error like this :

FATAL EXCEPTION: Thread-9
java.lang.RuntimeException: 
Can't create handler inside thread that has not called Looper.prepare()
    at android.os.Handler.<init>(Handler.java:121)
    at android.widget.Toast.<init>(Toast.java:68)
    at android.widget.Toast.makeText(Toast.java:231)
    at com.sat.alfaloc.MenuUtama.getRequest(MenuUtama.java:160)
    at com.sat.alfaloc.MenuUtama$1.run(MenuUtama.java:101)
    at java.lang.Thread.run(Thread.java:1096)

if activity save data in to server I command the progress bar can run,but if not this not worked..what should i do to fix this problem??

like image 815
akubabas Avatar asked Mar 15 '12 03:03

akubabas


People also ask

What kind of method prepare () is of Looper class?

Creating Looper and MessageQueue for a Thread: prepare() identifies the calling thread, creates a Looper and MessageQueue object and associate the thread with them in ThreadLocal storage class. Looper. loop() must be called to start the associated looper.

What is looper prepare?

↳ android.os.Looper. Class used to run a message loop for a thread. Threads by default do not have a message loop associated with them; to create one, call prepare() in the thread that is to run the loop, and then loop() to have it process messages until the loop is stopped.

What is the difference between handler and looper?

Looper is an abstraction over event loop (infinite loop which drains queue with events) and Handler is an abstraction to put/remove events into/from queue with events (which is drained by Looper) and handle these events when they are processed.

How many loopers and handlers can be attached to a thread?

There will be only one unique looper per thread. That means only one MessageQueue per thread. There can be any number of handlers for one single thread.


2 Answers

Probably you are getting error because of the thread in which you are using the context of the activity.

You should use the AsyncTask instead of a normal thread. In AsyncTask there is a method onPreExecute() and onPostExecute() which are executing on the main thread and there is a method doInBackground() which will execute on background so that you can easily implement the long live process.

You can refer this example

like image 116
Dharmendra Avatar answered Sep 27 '22 16:09

Dharmendra


Sometimes you might encounter this problem even when the offending code is inside the Activity (as in my case when using Unity game engine).

The following code has worked for me.

        this.runOnUiThread(new Runnable() {
           public void run() {
              //Your code here
              ...
           }
        });
like image 40
netlander Avatar answered Sep 27 '22 15:09

netlander