错误使用时的AsyncTask [英] error when use asynctask
问题描述
我有同样的数据库并行和相同的读写操作运行的两个过程。所以我想带他们到一个AsyncTask的,所以它们相互同步。但在实施的过程中,已经产生了,我不知道如何解决指望别人帮助的错误。
当你运行应用程序的的新同步()。执行(); 的错误
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.invoice);
doTimerTask();
}公共无效workoffline()
{
clearArray();
尝试{
mDb.openDB();
光标mCursor = mDb.getAllInvoice(IDDelivery);
如果(mCursor.moveToFirst()){
做{
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)); }而(mCursor.moveToNext());
loaddatalistview();
}
mDb.closeDB();
}赶上(例外五){
e.printStackTrace();
}}//奠定杜代TU服务器已经切BI
公共无效loaddata()
{ 字符串SAMPLEURL = SERVICE_URL +/monthlytarget.php
CallUrl WST =新CallUrl(CallUrl.GET_TASK,对此,外行丁字裤锡HOAđơn......,1,IDDelivery);
wst.execute(新的String [] {
SAMPLEURL
}); // HIEN THI丁字裤锡仁đượcLEN列表视图}私有类同步扩展的AsyncTask<太虚,太虚,太虚>
{ @覆盖
保护无效doInBackground(虚空...... PARAMS){
// TODO自动生成方法存根
loaddata();
返回null;
} @覆盖
保护无效onPostExecute(最终虚空未使用){
// TODO自动生成方法存根
workoffline();
}}//剑TRA切BICóKET NOI莽干草康元?
// NEUCó切BI SE林việc在线
// NEU清孔切BI SEchuyển唱大气温度động离线
公共无效的TestNetwork()
{
//剑TRA NOI KET服务器đến
TestConnectionNew测试=新TestConnectionNew();
尝试{
字符串收到= test.execute(http://longvansolution.tk/monthlytarget.php)获得();
如果(recieve.equalsIgnoreCase(真)及与放大器; isNetworkAvailable()==真)
{ // loaddata();
// workoffline();
新的同步()执行();
}
否则如果(recieve.equalsIgnoreCase(假)|| isNetworkAvailable()==假)
{
串乱=康元的KET NOIđến服务器乙酸切BI蔡CóKET NOI莽!;
Toast.makeText(Invoice.this,乱,Toast.LENGTH_SHORT).show();
workoffline();
} }赶上(InterruptedException的E){
e.printStackTrace();
}赶上(为ExecutionException E){
e.printStackTrace();
}
}//thựcHIEN负载数据TU服务器VE西奥的Thoi吉安địnhSAN
公共无效doTimerTask(){
TimerTask的mTimerTask;
TIMER T =新的Timer();
最后的处理程序处理程序=新的处理程序();
mTimerTask =新的TimerTask(){
公共无效的run(){
handler.post(新的Runnable(){
公共无效的run(){
的TestNetwork();
Log.d(TIMER,TimerTask的运行);
}
});
}
};
//公共无效时间表(TimerTask的任务,长时间延迟,周期长)
t.schedule(mTimerTask,500 300000); //
}
错误
14 12-21:43:54.750:E / AndroidRuntime(11093):致命异常:AsyncTask的#4
12-21 14:43:54.750:E / AndroidRuntime(11093):了java.lang.RuntimeException:执行doInBackground发生错误()
12-21 14:43:54.750:E / AndroidRuntime(11093):在android.os.AsyncTask $ 3.done(AsyncTask.java:278)
12-21 14:43:54.750:E / AndroidRuntime(11093):在java.util.concurrent.FutureTask中$ Sync.innerSetException(FutureTask.java:273)
12-21 14:43:54.750:E / AndroidRuntime(11093):在java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-21 14:43:54.750:E / AndroidRuntime(11093):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:307)
12-21 14:43:54.750:E / AndroidRuntime(11093):在java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-21 14:43:54.750:E / AndroidRuntime(11093):在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:208)
12-21 14:43:54.750:E / AndroidRuntime(11093):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-21 14:43:54.750:E / AndroidRuntime(11093):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:569)
12-21 14:43:54.750:E / AndroidRuntime(11093):在java.lang.Thread.run(Thread.java:856)
12-21 14:43:54.750:E / AndroidRuntime(11093):了java.lang.RuntimeException:所致。不能内螺纹已不叫尺蠖prepare创造处理器()
12-21 14:43:54.750:E / AndroidRuntime(11093):在android.os.Handler<&初始化GT;(Handler.java:121)
12-21 14:43:54.750:E / AndroidRuntime(11093):在android.app.Dialog<&初始化GT;(Dialog.java:107)
12-21 14:43:54.750:E / AndroidRuntime(11093):在android.app.AlertDialog<&初始化GT;(AlertDialog.java:114)
12-21 14:43:54.750:E / AndroidRuntime(11093):在android.app.AlertDialog<&初始化GT;(AlertDialog.java:98)
12-21 14:43:54.750:E / AndroidRuntime(11093):在android.app.ProgressDialog<&初始化GT;(ProgressDialog.java:77)
12-21 14:43:54.750:E / AndroidRuntime(11093):在Url.CallUrl.showProgressDialog(CallUrl.java:289)
12-21 14:43:54.750:E / AndroidRuntime(11093):在Url.CallUrl.on preExecute(CallUrl.java:300)
12-21 14:43:54.750:E / AndroidRuntime(11093):在android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
12-21 14:43:54.750:E / AndroidRuntime(11093):在android.os.AsyncTask.execute(AsyncTask.java:511)
12-21 14:43:54.750:E / AndroidRuntime(11093):在com.longvan.saigonfleamarket.Invoice.loaddata(Invoice.java:240)
12-21 14:43:54.750:E / AndroidRuntime(11093):在com.longvan.saigonfleamarket.Invoice $ sync.doInBackground(Invoice.java:254)
12-21 14:43:54.750:E / AndroidRuntime(11093):在com.longvan.saigonfleamarket.Invoice $ sync.doInBackground(Invoice.java:1)
12-21 14:43:54.750:E / AndroidRuntime(11093):在android.os.AsyncTask $ 2.call(AsyncTask.java:264)
12-21 14:43:54.750:E / AndroidRuntime(11093):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:305)
从你的UI线程(默认值)要创建一个的TimerTask
线程,从内部,你是创建一个的Runnable
(线程),并从内部要创建一个的AsyncTask
线程。在这之中,你有一个吐司
电话:
Toast.makeText(Invoice.this,乱,Toast.LENGTH_SHORT).show();
(你不能直接从这样的另一个线程访问UI线程;尝试处理)
我相信一些简化是为了;这里有一些很好的例子,应该帮助:
<一个href=\"http://stackoverflow.com/questions/4187960/asynctask-and-looper-$p$ppare-error/4217754#4217754\">AsyncTask和弯针。prepare()错误
<一个href=\"http://stackoverflow.com/questions/11264538/cant-create-handler-inside-thread-that-has-not-called-looper-$p$ppare-asynct\">Can't创建内螺纹的处理程序已经不叫尺蠖prepare() - AsyncTask的对话框里面
<一个href=\"http://stackoverflow.com/questions/10403858/java-cant-create-handler-inside-thread-that-has-not-called-looper-$p$ppare\">Java不能内螺纹创建处理程序,也没有所谓的活套。prepare()
定时器和放大器; TimerTask的线程与睡眠+ Java中
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. 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)
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. In the midst of this, you have a Toast
call:
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.)
I believe some simplification is in order; here are some excellent examples that should help:
AsyncTask and Looper.prepare() error
Can't create handler inside thread that has not called Looper.prepare() - AsyncTask inside a dialog
Java Can't create handler inside thread that has not called Looper.prepare()
Timer & TimerTask versus Thread + sleep in Java
这篇关于错误使用时的AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!