0

我有两个并行运行的进程,并且在同一个数据库上执行相同的读写操作。所以我想把它们带到一个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)
4

4 回答 4

0

您必须在 UI 线程中运行 UI 交互。尝试做

runOnUiThread(new Runnable() {

                @Override
                public void run() {

                    //your UI interaction code here

                }
            });

这将在 UI 线程中执行代码。

于 2012-12-21T08:14:09.943 回答
0

CallUrl 是否试图显示 ProgressDialog?如果是这样,您无法通过 AsyncTask 的 doInBackground 方法执行此操作。请查看 publishProgress 方法。

文档在这里。

于 2012-12-21T08:02:44.357 回答
0

在您的 UI 线程(默认)中,您正在创建一个TimerTask线程,在其中创建一个Runnable(线程),在其中您正在创建一个AsyncTask线程。在此过程中,您有一个Toast电话:

Toast.makeText(Invoice.this, mess, Toast.LENGTH_SHORT).show();

(您不能像这样直接从另一个线程访问 UI 线程;尝试使用处理程序。)

我相信一些简化是必要的;这里有一些很好的例子,应该会有所帮助:

AsyncTask 和 Looper.prepare() 错误

无法在未调用 Looper.prepare() 的线程内创建处理程序 - 对话框内的 AsyncTask

Java 无法在未调用 Looper.prepare() 的线程内创建处理程序

Java 中的 Timer & TimerTask 与 Thread + sleep

于 2012-12-21T08:26:27.640 回答
0

在调用异步任务之前创建一个 PorgressDialog 并在完成执行异步任务之后关闭 PorgressDialog。这将解决您的问题。在这里检查这里这里这里

于 2012-12-21T08:05:19.340 回答