我有一个Web服务,我从webservice获取数据,所以我有等待时间。为了解决等待,我更喜欢添加进度对话框。当我添加进度条时,项目没有工作。这是我的代码:
Runnable runnable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
setInfo();
}
};
//Toast.makeText(CardChooseActivity.this, adapter.getItem(position), Toast.LENGTH_SHORT).show();
(new Thread(runnable)).start();
}
我调用了setInfo函数,这是我的setInfo函数:
private void setInfo()
{
final ProgressDialog ringProgressDialog = ProgressDialog.show(ReportAllActivity.this, "Please wait ...", "Downloading Infos...", true);
ringProgressDialog.setCancelable(true);
new Thread(new Runnable() {
@Override
public void run() {
try {
String successMessage="";
jsonObject = new JSONObject();
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(URL);
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,10000);
HttpConnectionParams.setSoTimeout(httpParams,10000);
try {
jsonObject.put("data", "data1");
jsonObject.put("data1", "data2");
jsonObject.put("data2", "data3");
jsonObject.put("data3", "data4");
StringEntity stringEntity = new StringEntity(jsonObject.toString());
stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httpPost.setEntity(stringEntity);
CookieStore cookieStore = new BasicCookieStore();
cookieStore.addCookie(cookie);
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpResponse httpResponse = httpClient.execute(httpPost,localContext);
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
successMessage = EntityUtils.toString(httpResponse.getEntity());
Log.i("Report All activity", successMessage);
}
jArray = new JSONArray(successMessage);
runOnUiThread(new Runnable() {
public void run() {
fillTable(jArray);
}
});
}catch (IOException e){
Log.e("Login_Issue", e.toString());
}catch (JSONException e) {
e.printStackTrace();
}
} catch (Exception e) {
}
ringProgressDialog.dismiss();
}
}).start();
我的logcat错误:
09-07 10:37:17.156: E/AndroidRuntime(510): FATAL EXCEPTION: Thread-13
09-07 10:37:17.156: E/AndroidRuntime(510): java.lang.RuntimeException: Can't create handler
inside thread that has not called Looper.prepare()
09-07 10:37:17.156: E/AndroidRuntime(510): at android.os.Handler.<init>(Handler.java:121)
09-07 10:37:17.156: E/AndroidRuntime(510): at android.app.Dialog.<init>(Dialog.java:101)
09-07 10:37:17.156: E/AndroidRuntime(510): at android.app.AlertDialog.<init>
(AlertDialog.java:63)
09-07 10:37:17.156: E/AndroidRuntime(510): at android.app.ProgressDialog.<init>
(ProgressDialog.java:80)
09-07 10:37:17.156: E/AndroidRuntime(510): at android.app.ProgressDialog.<init>
(ProgressDialog.java:76)
09-07 10:37:17.156: E/AndroidRuntime(510): at
android.app.ProgressDialog.show(ProgressDialog.java:101)
09-07 10:37:17.156: E/AndroidRuntime(510): at
android.app.ProgressDialog.show(ProgressDialog.java:90)
09-07 10:37:17.156: E/AndroidRuntime(510): at
com.example.example.ReportAllActivity.setInfo(ReportAllActivity.java:119)
09-07 10:37:17.156: E/AndroidRuntime(510): at
com.example.example.ReportAllActivity.access$0(ReportAllActivity.java:117)
09-07 10:37:17.156: E/AndroidRuntime(510): at
com.example.example.ReportAllActivity$1.run(ReportAllActivity.java:109)
09-07 10:37:17.156: E/AndroidRuntime(510): at java.lang.Thread.run(Thread.java:1096)
答案 0 :(得分:1)
Android基本上适用于两种线程类型,即UI线程和后台线程。根据android文档 -
不要从UI线程外部访问Android UI工具包来解决此问题,Android提供了几种从其他线程访问UI线程的方法。
private void setInfo()
{
myactivity.this.runOnUiThread(new runnable()
{
public void run()
{
final ProgressDialog ringProgressDialog = ProgressDialog.show(ReportAllActivity.this, "Please wait ...", "Downloading Infos...", true);
ringProgressDialog.setCancelable(true);
}
});
/// Your other code goes here...
}