@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同步到DB。当我调试此进度条时,在上传所有数据后显示。在文本框中,current_status总是&#34; 100个数据从10&#34;的总数上传。但数据仅为10.如何在上传一个数据时更改文本框值。我在handler.post之前尝试了这两个与文本框相关的行(new 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)
答案 0 :(得分:0)
我建议在UI线程(runOnUiThread())上运行它来更新文本字段和UI组件。
其次,你遇到了重大的逻辑问题。 从您的初始代码,我假设您想要在跟踪状态时执行10个SyncTask。
从我可以看到的逻辑问题来看,这是我推荐的引导式重构工作流程(我不会直接为您的学习曲线提供解决方案):
应该从onClick方法中的for循环中取出以下跟踪。
@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);
}
// NOTE: The following should be refactored:
// It is better to create another AsyncTask for this tracking and updating the progress
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();
}
}
为String1(customer_id),String2(name)和String3(mobile1)创建一个包装类(例如:CustomerSyncDetails.class
)。
然后将所有10个客户详细信息放入一个数组(例如:arrayofCustomerSyncDetails),将包装器对象的数组传递给SyncTask。
在SyncTask doInBackground()方法中执行for循环。
new SyncTask().execute(arrayofCustomerSyncDetails);
对于跟踪线程,存在一个主要的逻辑缺陷,这是它显示上传的100个数据的主要原因。
while (current_status < 100) {
current_status += 1;
//sync_progress.setProgress(current_status);
...
我建议通过在AsyncTask和跟踪线程之间建立共享变量来重新考虑解决方案。