2013-02-26 47 views
0

在我的android应用程序中,我将一些数据从服务器数据库复制到本地数据库。它可能需要15分钟以上。对于较慢的连接,可能会超出。所以我想显示一个等待进度条。Android等待进度条无法在未调用Looper.prepare()的线程中创建处理程序错误

该代码张贴如下。

refresh.setOnClickListener(new OnClickListener(){ 
public void onClick(View v) { 
final ProgressDialog progressDialog = ProgressDialog.show(login.this, "", "Please wait..."); 
new Thread() { 
public void run() { 
try{ 
mySQLiteAdapter.openToWrite(); 
mySQLiteAdapter.deleteAll(); 
radapter.openToWrite(); 
radapter.deleteAll(); 
uadapter.openToWrite(); 
uadapter.deleteAll(); 
     try 
      { 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost("http://xxxxxxxxxxxxxxxxxxxxx"); 
       HttpResponse response = httpclient.execute(httppost); 
       HttpEntity entity = response.getEntity(); 
        is = entity.getContent(); 
      } catch (UnsupportedEncodingException e) { 
       e.printStackTrace(); 
      } catch (ClientProtocolException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      try { 
       BufferedReader reader = new BufferedReader(new InputStreamReader(
         is, "iso-8859-1"), 8); 
       StringBuilder sb = new StringBuilder(); 
       String line = null; 
       while ((line = reader.readLine()) != null) { 
        sb.append(line + "\n"); 
       } 
       is.close(); 
       json = sb.toString(); 
      } catch (Exception e) { 
      Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show(); 
      } 

      try { 
       jObj = new JSONObject(json); 
       contacts = jObj.getJSONArray("get"); 
       for(int i = 0; i < contacts.length(); i++){ 
       JSONObject c = contacts.getJSONObject(i); 
        if(!c.isNull("rname"))// || c.getString("rcode").equals(null)) 
        { 
         rname = c.getString("rname"); 
         rcode = c.getString("rcode"); 
         radapter.insert(rcode, rname); 
        } 
        else 
        { 
         rname = ""; 
         rcode = c.getString("rcode"); 
         radapter.insert(rcode, rname); 
        } 
       } 
       Toast.makeText(getBaseContext(), " Ryot BackUp completed", Toast.LENGTH_SHORT).show(); 

      } catch (JSONException e) { 
       Toast.makeText(getBaseContext(), "Error"+e.toString(), Toast.LENGTH_LONG).show(); 
      } 
        progressDialog.dismiss(); 
       } 
      }.start(); 
      adapter.deleteAll(); 
      oadapter.deleteAll(); 
      e2.setText("Back Up completed"); 
     } 
      catch (Exception e) { 
       Log.e("tag", e.getMessage()); 
       } 
        progressDialog.dismiss(); 
       } 
      }.start(); 
     } 

    }); 

当我跑步时我的应用程序时,会显示进度条,仅几秒钟,并执行Catch块,显示logcat的误差

的logcat:

02-26 14:26:07.151: E/tag(301): Can't create handler inside thread that has not called Looper.prepare() 

可有人解释我的代码中的错误以及如何解决它

+0

试图从另一个线程访问ui的典型情况。解决方案是使用asynctask作为背景材料并在onProgressUpdate和onPostExecute中访问ui。 – njzk2 2013-02-26 09:11:45

回答

0
Toast.makeText(getBaseContext(), " Ryot BackUp completed", Toast.LENGTH_SHORT).show(); 

问题是ca由上面的代码使用,这个方法会改变UI,你不能在非UI线程中更新UI。所以你需要从主UI线程调用它。

由于这是一项耗时的工作,我强烈建议您在Android中使用AsyncTask

但是,如果你只是想做出一点改变现有的代码,尝试使用

context.runOnUiThread(new Runnable() { 
    public void run() { 
    Toast.makeText(...).show(); 
    } 
}); 

或处理,你可以在How Handler Work in Android看一看。一个基本的例子:

public Handler mHandler = new Handler() { 

    public void handleMessage(Message msg) { 
      //show toast here 
    } 
} 

protected void startLongRunningOperation() { 

    Thread t = new Thread() { 
     public void run() { 
      //do something here 
      //update the result 
      mHandler.postDelayed(mUpdateResults, 200);} 
      mHandler.post(mUpdateResults); 
     } 
    }; 
    t.start(); 
} 
+0

如何使用处理程序,你可以发布示例代码 – 2013-02-26 09:08:31

+0

我应该在哪里使用?仅用于显示烤面包 – 2013-02-26 09:12:19

+0

更新了答案。 – StarPinkER 2013-02-26 09:19:57

相关问题