@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_sync:
for (int i = 0; i<10; i++) {
current_status = i;
new SyncTask().execute(String1,String2,String3);
new Thread(new Runnable() {
public void run() {
while (current_status < 100) {
current_status += 1;
//sync_progress.setProgress(current_status);
//sync_decrip.setText(current_status+" data is uploading from total of "+c_id.size());
handler.post(new Runnable() {
public void run() {
sync_progress.setProgress(current_status);
sync_decrip.setText(current_status + " data is uploading from total of " + c_id.size());
}
});
try {
// Sleep for 200 milliseconds.
//Just to display the progress slowly
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
}
public class SyncTask extends AsyncTask<String, Void, String> {
ProgressDialog progressDialog;
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... arg0) {
String result=null;
try{
// url where the data will be posted
String postReceiverUrl = "http://MyUrl";
Log.v(TAG, "postURL: " + postReceiverUrl);
HttpClient httpClient = new DefaultHttpClient(); // HttpClient
HttpPost httpPost = new HttpPost(postReceiverUrl); // post header
// add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
//Log.d(uname.getText().toString(), pass.getText().toString());
nameValuePairs.add(new BasicNameValuePair("customer_id", arg0[0]));
nameValuePairs.add(new BasicNameValuePair("name", arg0[1]));
nameValuePairs.add(new BasicNameValuePair("mobile1", arg0[2]));
// execute HTTP post request
HttpResponse response = httpClient.execute(httpPost);
result = EntityUtils.toString(response.getEntity());
}
catch (Exception e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String result) {
// do stuff after posting data
//progressDialog.dismiss();
String message=null;
try {
Log.d("Inside Post", "message");
root = new JSONObject(result);
validation = root.getInt("response");
message = root.getString("CustomerName");
message+=root.getString("MobileNumber");
up_state=root.getString("MobileNumber");
Log.d(result, user_id);
//Toast.makeText(getApplicationContext(), user_id, Toast.LENGTH_LONG).show();
if(validation==1) {
Log.e(message, root.getString("Emailid"));
//Toast.makeText(getApplicationContext(),"Success: "+ message, Toast.LENGTH_LONG).show();
}
// else
// Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我试图将数据从sqlite同步到带有进度条的数据库。当我调试这个进度条时,在所有数据上传后显示。在文本框中,current_status总是“100个数据从总共10个上传”。但数据只有10个。如何在上传一个数据时更改文本框的值。我试着用handler.post之前,这两个文本框,相关线(新的Runnable(){,但应用程序与下面的日志崩溃。进度条在所有工作完成后运行
12-25 12:19:45.909: E/AndroidRuntime(21914): FATAL EXCEPTION: Thread-981
12-25 12:19:45.909: E/AndroidRuntime(21914): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
12-25 12:19:45.909: E/AndroidRuntime(21914): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4077)
12-25 12:19:45.909: E/AndroidRuntime(21914): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:734)
12-25 12:19:45.909: E/AndroidRuntime(21914): at android.view.View.requestLayout(View.java:12679)
12-25 12:19:45.909: E/AndroidRuntime(21914): at android.view.View.requestLayout(View.java:12679)
12-25 12:19:45.909: E/AndroidRuntime(21914): at android.view.View.requestLayout(View.java:12679)
12-25 12:19:45.909: E/AndroidRuntime(21914): at android.view.View.requestLayout(View.java:12679)
12-25 12:19:45.909: E/AndroidRuntime(21914): at android.view.View.requestLayout(View.java:12679)
12-25 12:19:45.909: E/AndroidRuntime(21914): at android.view.View.requestLayout(View.java:12679)
12-25 12:19:45.909: E/AndroidRuntime(21914): at android.widget.TextView.checkForRelayout(TextView.java:6773)
12-25 12:19:45.909: E/AndroidRuntime(21914): at android.widget.TextView.setText(TextView.java:3306)
12-25 12:19:45.909: E/AndroidRuntime(21914): at android.widget.TextView.setText(TextView.java:3162)
12-25 12:19:45.909: E/AndroidRuntime(21914): at android.widget.TextView.setText(TextView.java:3137)
12-25 12:19:45.909: E/AndroidRuntime(21914): at ridio.helixtech.SyncClass$1.run(SyncClass.java:121)
12-25 12:19:45.909: E/AndroidRuntime(21914): at java.lang.Thread.run(Thread.java:856)
你可以请发布整个代码?我不清楚你想要做什么。 –
你不能在线程中处理视图(文本视图),你需要使用ui线程。 – Harry