android – java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序

前端之家收集整理的这篇文章主要介绍了android – java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
嗨,我的代码有问题.我的代码
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);

和这样的错误

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)

如果活动将数据保存到服务器我命令进度条可以运行,但如果没有这不起作用..我应该怎么做才能解决这个问题?

解决方法

由于您正在使用活动上下文的线程,您可能会收到错误.

您应该使用AsyncTask而不是普通线程.
在AsyncTask中,有一个onPreExecute()和onPostExecute()方法,它们在主线程上执行,并且有一个方法doInBackground()将在后台执行,以便您可以轻松实现长期进程.

你可以参考this example

原文链接:https://www.f2er.com/android/317488.html

猜你在找的Android相关文章