Asynctask和onclicklistener android 2.3.3与3.2

时间:2011-09-25 23:01:20

标签: android onclick android-asynctask

在我的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取消进度对话框?

1 个答案:

答案 0 :(得分:0)

为什么在不希望它出现时调用setCancelable(true)?也许这会解决你的问题......

只是一个提示:

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

}
  1. 你为什么这样做?
  2. try ... catch中的代码包含在一般异常捕获而没有反应的情况下是一个非常糟糕的习惯。当你这样做时,你应该总是留下一个充分理由的评论。