Asynctask 和 onclicklistener android 2.3.3 与 3.2

发布于 2024-12-06 07:56:05 字数 2821 浏览 0 评论 0原文

在我的 Nexus One (android 2.3.3) 上,以下程序的行为与我的 xoom (android 3.2) 不同:

public class TestOnclickWithAsyncTask extends Activity {

private int mClicked = 0;

private final static String TAG = "TestOnclickWithAsyncTask";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.main);
    RelativeLayout relativeLayout= (RelativeLayout) findViewById(R.id.img_control_panel);

    final Button button = new Button(this);

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            mClicked++;
            Log.i (TAG, "Clicked "+ mClicked + " times");
        }
    });

    relativeLayout.addView(button);


    new LongTask().execute();

}

private class LongTask extends AsyncTask<Void , Void, Void> {
    private ProgressDialog dialog;

      protected void onPreExecute() {
      dialog = new  ProgressDialog(TestOnclickWithAsyncTask.this);
      dialog.setMessage("Waiting ");
      dialog.setIndeterminate(true); 
      dialog.setCancelable(true); 
      try {
          dialog.show();
      } catch (Exception e){}
      }

        protected Void doInBackground(Void... notused) {
                try {
                    Thread.sleep (30000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }//do long task
            return null;
        }


        protected void onPostExecute(Void unusedg) { 
            dialog.dismiss();
            Log.i (TAG,"Long task finished");
        }

}

}

在我的 Nexus 上,我无法在 asynctask 运行时按下按钮(所需的行为):

09-26 00:45:34.361: INFO/TestOnclickWithAsyncTask(3803): Long task finished
09-26 00:45:36.463: INFO/TestOnclickWithAsyncTask(3803): Clicked 1 times
09-26 00:45:36.814: INFO/TestOnclickWithAsyncTask(3803): Clicked 2 times
09-26 00:45:37.975: INFO/TestOnclickWithAsyncTask(3803): Clicked 3 times
09-26 00:45:38.435: INFO/TestOnclickWithAsyncTask(3803): Clicked 4 times

在我的 xoom 上,我只需按按钮和进度指示器消失。

09-26 00:49:05.440: INFO/TestOnclickWithAsyncTask(5887): Clicked 1 times
09-26 00:49:06.230: INFO/TestOnclickWithAsyncTask(5887): Clicked 2 times
09-26 00:49:06.580: INFO/TestOnclickWithAsyncTask(5887): Clicked 3 times
09-26 00:49:06.840: INFO/TestOnclickWithAsyncTask(5887): Clicked 4 times
09-26 00:49:08.170: INFO/TestOnclickWithAsyncTask(5887): Clicked 5 times
09-26 00:49:08.560: INFO/TestOnclickWithAsyncTask(5887): Clicked 6 times
09-26 00:49:30.960: INFO/TestOnclickWithAsyncTask(5887): Long task finished 
09-26 00:49:32.800: INFO/TestOnclickWithAsyncTask(5887): Clicked 7 times
09-26 00:49:33.810: INFO/TestOnclickWithAsyncTask(5887): Clicked 8 times

为什么会有所不同?更重要的是,如何防止 xoom 取消进度对话框?

On my nexus One (android 2.3.3) the following program behaves differently from my xoom (android 3.2):

public class TestOnclickWithAsyncTask extends Activity {

private int mClicked = 0;

private final static String TAG = "TestOnclickWithAsyncTask";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.main);
    RelativeLayout relativeLayout= (RelativeLayout) findViewById(R.id.img_control_panel);

    final Button button = new Button(this);

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            mClicked++;
            Log.i (TAG, "Clicked "+ mClicked + " times");
        }
    });

    relativeLayout.addView(button);


    new LongTask().execute();

}

private class LongTask extends AsyncTask<Void , Void, Void> {
    private ProgressDialog dialog;

      protected void onPreExecute() {
      dialog = new  ProgressDialog(TestOnclickWithAsyncTask.this);
      dialog.setMessage("Waiting ");
      dialog.setIndeterminate(true); 
      dialog.setCancelable(true); 
      try {
          dialog.show();
      } catch (Exception e){}
      }

        protected Void doInBackground(Void... notused) {
                try {
                    Thread.sleep (30000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }//do long task
            return null;
        }


        protected void onPostExecute(Void unusedg) { 
            dialog.dismiss();
            Log.i (TAG,"Long task finished");
        }

}

}

On my Nexus I can't press the button while the asynctask is running (desired behavior):

09-26 00:45:34.361: INFO/TestOnclickWithAsyncTask(3803): Long task finished
09-26 00:45:36.463: INFO/TestOnclickWithAsyncTask(3803): Clicked 1 times
09-26 00:45:36.814: INFO/TestOnclickWithAsyncTask(3803): Clicked 2 times
09-26 00:45:37.975: INFO/TestOnclickWithAsyncTask(3803): Clicked 3 times
09-26 00:45:38.435: INFO/TestOnclickWithAsyncTask(3803): Clicked 4 times

On my xoom I can just press the button and the progress indicator disappears.

09-26 00:49:05.440: INFO/TestOnclickWithAsyncTask(5887): Clicked 1 times
09-26 00:49:06.230: INFO/TestOnclickWithAsyncTask(5887): Clicked 2 times
09-26 00:49:06.580: INFO/TestOnclickWithAsyncTask(5887): Clicked 3 times
09-26 00:49:06.840: INFO/TestOnclickWithAsyncTask(5887): Clicked 4 times
09-26 00:49:08.170: INFO/TestOnclickWithAsyncTask(5887): Clicked 5 times
09-26 00:49:08.560: INFO/TestOnclickWithAsyncTask(5887): Clicked 6 times
09-26 00:49:30.960: INFO/TestOnclickWithAsyncTask(5887): Long task finished 
09-26 00:49:32.800: INFO/TestOnclickWithAsyncTask(5887): Clicked 7 times
09-26 00:49:33.810: INFO/TestOnclickWithAsyncTask(5887): Clicked 8 times

Why does this differ? And more importantly how can I prevent the xoom from canceling the progress dialog?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩世 2024-12-13 07:56:05

当您不希望这样时,为什么还要调用 setCancelable(true) 呢?也许这会解决您的问题...

只是一个提示:

try {
    dialog.show();
} catch (Exception e) {

}
  1. 您为什么要这样做?
  2. 将代码包含在 try ... catch 中,并使用一般异常捕获而不做出任何反应,这是一个非常糟糕的习惯。当您这样做时,您应该始终留下评论并给出充分的理由。

Why do you call setCancelable(true) when you don't want it to be that way? Maybe this will solve your issue...

Just a hint:

try {
    dialog.show();
} catch (Exception e) {

}
  1. Why are you doing that?
  2. Enclosing code in try ... catch with a general exception catch without a reaction is a really bad habit. When you do that you should always leave a comment with a good reason for that.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文