[英]error when use asynctask
I have two processes running in parallel and the same read and write operations on the same database. 我有两个并行运行的进程,并且在同一数据库上执行相同的读取和写入操作。 So I want to take them to a asynctask, so they sync with each other .But in the process of implementation, has generated an error that I do not know how fix expect people to help.
因此,我想将它们带到asynctask,以便它们彼此同步。但是在实施过程中,产生了一个错误,我不知道如何修复期望人们提供帮助。 when you run the application to the new sync ().
当您将应用程序运行到新的同步()时。 execute ();
执行 (); error
错误
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); //
}
error 错误
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)
You'll have to run the UI interactions in the UI thread. 您必须在UI线程中运行UI交互。 Try doing
尝试做
runOnUiThread(new Runnable() {
@Override
public void run() {
//your UI interaction code here
}
});
This will execute the code in the UI thread. 这将在UI线程中执行代码。
From your UI thread (default) you are creating a TimerTask
thread, and from within that you are creating a Runnable
(thread), and from within that you are creating an AsyncTask
thread. 从UI线程(默认)创建一个
TimerTask
线程,从中创建Runnable
(线程),从中创建AsyncTask
线程。 In the midst of this, you have a Toast
call: 在此过程中,您有一个
Toast
调用:
Toast.makeText(Invoice.this, mess, Toast.LENGTH_SHORT).show();
(You can't access the UI thread directly from another thread like this; try a handler.) (您不能像这样直接从另一个线程访问UI线程;请尝试使用处理程序。)
I believe some simplification is in order; 我认为必须进行一些简化; here are some excellent examples that should help:
这里有一些很好的例子,应该会有所帮助:
AsyncTask and Looper.prepare() error AsyncTask和Looper.prepare()错误
Can't create handler inside thread that has not called Looper.prepare() - AsyncTask inside a dialog 无法在尚未调用Looper.prepare()的线程内创建处理程序-对话框内的AsyncTask
Java Can't create handler inside thread that has not called Looper.prepare() Java无法在尚未调用Looper.prepare()的线程内创建处理程序
Timer & TimerTask versus Thread + sleep in Java Timer和TimerTask与Java中的线程+睡眠
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.