1

我有一个按下按钮的类,它显示一个对话框。此框有两个按钮是,否。当我单击“是”时,我想调用异步任务来更新 DB 中的一些值。我试过这个,但它给了我 Looper.prepare() 错误。在 SO 和 GOogle 上进行了全面搜索,但没有解决方案。any1有什么想法吗?

谢谢

bttnSync.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            if (isOnline()) {
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        MainPage.this);
                alertDialogBuilder.setTitle("Syncing all data....!!");
                alertDialogBuilder
                        .setMessage(
                                "Are you sure you want to Sync all the data?")
                        .setCancelable(false)
                        .setPositiveButton("Yes",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(
                                            DialogInterface dialog, int id) {

                                         //call();

                                        new CountDownTask().execute();

                                        MainPage.this.finish();
                                        Intent i = new Intent(
                                                "com.example.collegesoft.MainPage");
                                        startActivity(i);
                                    }
                                })
                        .setNegativeButton("No",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(
                                            DialogInterface dialog, int id) {
                                        dialog.cancel();
                                    }
                                });

                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();
            } else {
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        MainPage.this);
                alertDialogBuilder.setTitle("No Internet Connection!!");
                alertDialogBuilder
                        .setMessage(
                                "Device is not connected to the Internet or the connection is slow.")
                        .setCancelable(false)

                        .setNegativeButton("Cancel",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(
                                            DialogInterface dialog, int id) {
                                        dialog.cancel();
                                    }
                                });

                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();
            }

        }
    });


private class CountDownTask extends AsyncTask<Void, Integer, Void> {

    protected void onPreExecute() {
        progressDialog = ProgressDialog.show(MainPage.this, "Loading...",
                "Loading application , please wait...", false, false);

    }

    protected Void doInBackground(Void... params) {
        call();


        return null;
    }

    protected void onProgressUpdate(Integer... values) {
        progressDialog.setProgress(values[0]);
    }

    protected void onPostExecute(Void result) {
        progressDialog.dismiss();
    }

}


   01-31 10:41:37.575: E/AndroidRuntime(9038): FATAL EXCEPTION: AsyncTask #1
   01-31 10:41:37.575: E/AndroidRuntime(9038): java.lang.RuntimeException: An error   occured while executing doInBackground()
   01-31 10:41:37.575: E/AndroidRuntime(9038):  at android.os.AsyncTask$3.done(AsyncTask.java:278)
     01-31 10:41:37.575: E/AndroidRuntime(9038):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
      01-31 10:41:37.575: E/AndroidRuntime(9038):   at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
     01-31 10:41:37.575: E/AndroidRuntime(9038):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
    01-31 10:41:37.575: E/AndroidRuntime(9038):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
     01-31 10:41:37.575: E/AndroidRuntime(9038):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
       01-31 10:41:37.575: E/AndroidRuntime(9038):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
     01-31 10:41:37.575: E/AndroidRuntime(9038):    at java.lang.Thread.run(Thread.java:856)
    01-31 10:41:37.575: E/AndroidRuntime(9038): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
     01-31 10:41:37.575: E/AndroidRuntime(9038):    at android.os.Handler.<init>(Handler.java:121)
     01-31 10:41:37.575: E/AndroidRuntime(9038):    at android.app.Activity.<init>(Activity.java:735)
     01-31 10:41:37.575: E/AndroidRuntime(9038):    at com.example.collegesoft.SyncData.<init>(SyncData.java:22)
     01-31 10:41:37.575: E/AndroidRuntime(9038):    at com.example.collegesoft.MainPage.call(MainPage.java:259)
     01-31 10:41:37.575: E/AndroidRuntime(9038):    at com.example.collegesoft.MainPage$CountDownTask.doInBackground(MainPage.java:284)
     01-31 10:41:37.575: E/AndroidRuntime(9038):    at com.example.collegesoft.MainPage$CountDownTask.doInBackground(MainPage.java:1)
       01-31 10:41:37.575: E/AndroidRuntime(9038):  at android.os.AsyncTask$2.call(AsyncTask.java:264)
      01-31 10:41:37.575: E/AndroidRuntime(9038):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
   01-31 10:41:37.575: E/AndroidRuntime(9038):  ... 4 more
    01-31 10:41:43.966: E/WindowManager(9038): Activity  com.example.collegesoft.MainPage has leaked window  com.android.internal.policy.impl.PhoneWindow$DecorView@41753818 that was originally added  here
     01-31 10:41:43.966: E/WindowManager(9038): android.view.WindowLeaked: Activity com.example.collegesoft.MainPage has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41753818 that was originally added here
     01-31 10:41:43.966: E/WindowManager(9038):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:383)
    01-31 10:41:43.966: E/WindowManager(9038):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:279)
    01-31 10:41:43.966: E/WindowManager(9038):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
  01-31 10:41:43.966: E/WindowManager(9038):    at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
    01-31 10:41:43.966: E/WindowManager(9038):  at android.view.Window$LocalWindowManager.addView(Window.java:537)
     01-31 10:41:43.966: E/WindowManager(9038):     at android.app.Dialog.show(Dialog.java:278)
   01-31 10:41:43.966: E/WindowManager(9038):   at android.app.ProgressDialog.show(ProgressDialog.java:116)
    01-31 10:41:43.966: E/WindowManager(9038):  at android.app.ProgressDialog.show(ProgressDialog.java:104)
     01-31 10:41:43.966: E/WindowManager(9038):     at com.example.collegesoft.MainPage$CountDownTask.onPreExecute(MainPage.java:278)
     01-31 10:41:43.966: E/WindowManager(9038):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
    01-31 10:41:43.966: E/WindowManager(9038):  at android.os.AsyncTask.execute(AsyncTask.java:511)
   01-31 10:41:43.966: E/WindowManager(9038):   at com.example.collegesoft.MainPage$3$1$1.run(MainPage.java:82)
       01-31 10:41:43.966: E/WindowManager(9038):   at android.os.Handler.handleCallback(Handler.java:605)
       01-31 10:41:43.966: E/WindowManager(9038):   at android.os.Handler.dispatchMessage(Handler.java:92)
    01-31 10:41:43.966: E/WindowManager(9038):  at android.os.Looper.loop(Looper.java:137)
      01-31 10:41:43.966: E/WindowManager(9038):    at android.app.ActivityThread.main(ActivityThread.java:4514)
   01-31 10:41:43.966: E/WindowManager(9038):   at java.lang.reflect.Method.invokeNative(Native Method)
   01-31 10:41:43.966: E/WindowManager(9038):   at java.lang.reflect.Method.invoke(Method.java:511)
  01-31 10:41:43.966: E/WindowManager(9038):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
   01-31 10:41:43.966: E/WindowManager(9038):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
     01-31 10:41:43.966: E/WindowManager(9038):     at dalvik.system.NativeStart.main(Native Method)

我的 call() 方法....

protected void call() {
    Log.d(TAG,"Call Called");
    myDbHelper = new DatabaseHelper(MainPage.this);

    try {

        myDbHelper.openDataBase();

    } catch (SQLException sqle) {

        throw sqle;

    }

    Cursor c = myDbHelper.getAllStudentFromMainTable();

    Log.d(TAG, "Cursor : " + c.getColumnCount());

    //new CountDownTask().execute();
    new SyncData().sendJson(c, myDbHelper, MainPage.this);

    //c.close();

}
4

3 回答 3

1

您可以在您的活动中创建一个处理程序,然后使用它来发布您需要做的事情。

例子:

public void onAvatarChange(View v) {
   final Handler uiHandler = new Handler();

   //Build dialog....
   OnclickListener onYes = new OnClickListener {
       public void onClick(View v) {
            uiHanlder.post(new Runnable() {
                 public void run() {
                     //Do whatever you want here...
                     //You will have looper.prepare here, as it will run on main thread as soon as possible.
                 }
            }
       }
   }

}
于 2013-01-31T10:26:59.527 回答
0

这怎么可能?

new CountDownTask().execute();

MainPage.this.finish();
Intent i = new Intent("com.example.collegesoft.MainPage");                                              
startActivity(i);

我的意思是,在某一时刻,您正在执行 AsyncTask,同时您正在开始新的活动。所以它给你错误只是因为你在 CountDownTask 中显示了 ProgressDialog。

解决方案:

相反,建议的解决方案是在 CountDownTask() 的 onPostExecute() 方法中包含 startActivity() 代码:

protected void onPostExecute(Void result) {
  progressDialog.dismiss();

  MainPage.this.finish();
  Intent i = new Intent("com.example.collegesoft.MainPage");                                              
  startActivity(i);

}
于 2013-01-31T10:30:56.777 回答
0
runOnUiThread(new Runnable() {
        @Override
        public void run() {

            // start your asynch task here

        }
    });
于 2013-01-31T10:36:25.517 回答