0

私の ProgressDialog は、 oncreate メソッドで show() を使用する場合に機能します。

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        pd = ProgressDialog.show(this, "Working", "Retreiving Neaby Restaurants", true, true);  // initializes the progress dialog
        pd.setCanceledOnTouchOutside(false);  // on newer versions of android touching outside of the screen will close the dialog. I decided to make it only cancelable when the back button is pressed.
        setContentView(R.layout.nearby_places_list);

ただし、on create() ではないため、非同期タスクで使用するとダイアログ コードが機能しません。引数 show(this, "Working", "Retreiving Neaby Restaurants", true, true) は、onCreate() の外では許可されていません。

class MyAsync extends AsyncTask<Void, Integer, Boolean>{  // this task is for populating the listview

        @Override 
        protected void onPreExecute() {
            //ProgressDialog.show(context, "Working", "Retreiving Neaby Restaurants");
            pd = ProgressDialog.show(this, "Working", "Retreiving Neaby Restaurants", true, true);  // initializes the progress dialog
            pd.setCanceledOnTouchOutside(false);  // on newer versions of android touching outside of the screen will close the dialog. I decided to make it only cancelable when the back button is pressed.
        }

非同期タスクの開始時にダイアログを表示したかったのですが、そのタスクが呼び出される少し前に、これはやや動揺しています。

したがって、私の質問は、ユーザーが実際に非同期タスクの使用を開始するまで、onPreExecute() の非同期タスク内でこれを機能させる方法です。

4

1 に答える 1

1

使用する

pd = ProgressDialog.show(yourActivitName.this, "Working", "Retreiving Neaby Restaurants", true, true);

onPreExecute()

これは実際にContextはアクティビティの です。今後も頻繁に使用する必要がある重要なもの。

Context activityContextを作成することもできますClass level variable

このように後で初期化しsetContentViewますonCreate

activityContext=this

そして、あなたも使用することができます

pd = ProgressDialog.show(activityContext, "Working", "Retreiving Neaby Restaurants", true, true);

于 2012-11-27T18:45:34.760 に答える