我有两个并行运行的进程,并且在同一个数据库上执行相同的读写操作。所以我想把它们带到一个asynctask,让它们相互同步。但是在执行过程中,产生了一个错误,我不知道如何修复希望人们提供帮助。当你运行应用程序到新的sync()。执行 (); 错误
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.invoice);
doTimerTask();
}
public void workoffline()
{
clearArray();
try {
mDb.openDB();
Cursor mCursor = mDb.getAllInvoice(IDDelivery);
if (mCursor.moveToFirst()) {
do {
codeInvoiceArray.add(mCursor.getString(1));
nameArray.add(mCursor.getString(2));
phonenumberArray.add(mCursor.getString(3));
addressArray.add(mCursor.getString(4));
urlArray.add(mCursor.getString(5));
} while (mCursor.moveToNext());
loaddatalistview();
}
mDb.closeDB();
} catch (Exception e) {
e.printStackTrace();
}
}
// lấy dữ liệu từ server về thiết bị
public void loaddata()
{
String sampleURL = SERVICE_URL + "/monthlytarget.php";
CallUrl wst = new CallUrl(CallUrl.GET_TASK, this, "Lấy thông tin hóa đơn...", 1, IDDelivery);
wst.execute(new String[] {
sampleURL
});
// Hiển thị thông tin nhận được lên listview
}
private class sync extends AsyncTask<Void, Void, Void>
{
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
loaddata();
return null;
}
@Override
protected void onPostExecute(final Void unused) {
// TODO Auto-generated method stub
workoffline();
}
}
// kiểm tra thiết bị có kết nối mạng hay không?
// nếu có thiết bị sẽ làm việc online
// nếu không thiết bị sẽ chuyển sang hoạt động offline
public void testNetwork()
{
// Kiểm tra kết nối đến server
TestConnectionNew test = new TestConnectionNew();
try {
String recieve = test.execute("http://longvansolution.tk/monthlytarget.php").get();
if (recieve.equalsIgnoreCase("true") && isNetworkAvailable() == true)
{
//loaddata();
// workoffline();
new sync().execute();
}
else if (recieve.equalsIgnoreCase("false") || isNetworkAvailable() == false)
{
String mess = "Không thể kết nối đến server hoặc thiết bị chưa có kết nối mạng!";
Toast.makeText(Invoice.this, mess, Toast.LENGTH_SHORT).show();
workoffline();
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
// thực hiện load data từ server về theo thời gian định sẵn
public void doTimerTask() {
TimerTask mTimerTask;
Timer t = new Timer();
final Handler handler = new Handler();
mTimerTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
testNetwork();
Log.d("TIMER", "TimerTask run");
}
});
}
};
// public void schedule (TimerTask task, long delay, long period)
t.schedule(mTimerTask, 500, 300000); //
}
错误
12-21 14:43:54.750: E/AndroidRuntime(11093): FATAL EXCEPTION: AsyncTask #4
12-21 14:43:54.750: E/AndroidRuntime(11093): java.lang.RuntimeException: An error occured while executing doInBackground()
12-21 14:43:54.750: E/AndroidRuntime(11093): at android.os.AsyncTask$3.done(AsyncTask.java:278)
12-21 14:43:54.750: E/AndroidRuntime(11093): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-21 14:43:54.750: E/AndroidRuntime(11093): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-21 14:43:54.750: E/AndroidRuntime(11093): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-21 14:43:54.750: E/AndroidRuntime(11093): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-21 14:43:54.750: E/AndroidRuntime(11093): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
12-21 14:43:54.750: E/AndroidRuntime(11093): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-21 14:43:54.750: E/AndroidRuntime(11093): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-21 14:43:54.750: E/AndroidRuntime(11093): at java.lang.Thread.run(Thread.java:856)
12-21 14:43:54.750: E/AndroidRuntime(11093): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
12-21 14:43:54.750: E/AndroidRuntime(11093): at android.os.Handler.<init>(Handler.java:121)
12-21 14:43:54.750: E/AndroidRuntime(11093): at android.app.Dialog.<init>(Dialog.java:107)
12-21 14:43:54.750: E/AndroidRuntime(11093): at android.app.AlertDialog.<init>(AlertDialog.java:114)
12-21 14:43:54.750: E/AndroidRuntime(11093): at android.app.AlertDialog.<init>(AlertDialog.java:98)
12-21 14:43:54.750: E/AndroidRuntime(11093): at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
12-21 14:43:54.750: E/AndroidRuntime(11093): at Url.CallUrl.showProgressDialog(CallUrl.java:289)
12-21 14:43:54.750: E/AndroidRuntime(11093): at Url.CallUrl.onPreExecute(CallUrl.java:300)
12-21 14:43:54.750: E/AndroidRuntime(11093): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
12-21 14:43:54.750: E/AndroidRuntime(11093): at android.os.AsyncTask.execute(AsyncTask.java:511)
12-21 14:43:54.750: E/AndroidRuntime(11093): at com.longvan.saigonfleamarket.Invoice.loaddata(Invoice.java:240)
12-21 14:43:54.750: E/AndroidRuntime(11093): at com.longvan.saigonfleamarket.Invoice$sync.doInBackground(Invoice.java:254)
12-21 14:43:54.750: E/AndroidRuntime(11093): at com.longvan.saigonfleamarket.Invoice$sync.doInBackground(Invoice.java:1)
12-21 14:43:54.750: E/AndroidRuntime(11093): at android.os.AsyncTask$2.call(AsyncTask.java:264)
12-21 14:43:54.750: E/AndroidRuntime(11093): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)