0

我试图在填充布局时显示进度对话框:

编辑(这里是代码):

public class CalendarMonth extends Activity {

    private ProgressDialog mDialog;

        private int mMonth = 0;
    private int mYear = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calendar_m);

        filterData();
    }

    private void filterData() {

        mDialog = ProgressDialog.show(this, "Loading calendar", 
                "Please wait...", true, true);
        mDialog.setCancelable(false);
        mDialog.show();

        new GenerateMonth().execute();
    }

class GenerateMonth extends AsyncTask {
        protected Object doInBackground(Object... arg0) {
            generateMonth();
            return true;
        }
        protected void onPostExecute(Object result) {
            mDialog.dismiss();
        }

    private void generateMonth() {

        mCalendar = new CalendarInstance(mMonth, mYear);

        mMonth = this.mCalendar.getSelectedMonth();
        mYear = this.mCalendar.getSelectedYear();

        TextView monthName = (TextView) findViewById(R.id.monthName);
        monthName.setText(mCalendar.getMonthAndYearAsString());

        Button monthPrevious = (Button) findViewById(R.id.monthPrevious);
        monthPrevious.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                int month = mMonth - 1;
                int year = mYear;
                if (mMonth == 1) {
                    month = 12;
                    year = mYear - 1;
                }
                mMonth = month;
                mYear = year;
                filterData();
            }
        });

        Button monthNext = (Button) findViewById(R.id.monthNext);
        monthNext.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                int month = mMonth + 1;
                int year = mYear;
                if (mMonth == 12) {
                    month = 1;
                    year = mYear + 1;
                }
                mMonth = month;
                mYear = year;
                filterData();
            }
        });

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setNumColumns(7);
        gridview.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
        gridview.setBackgroundColor(Color.BLACK);
        gridview.setAdapter(new CalendarMonthAdapter(this, mMonth, mYear));     
    }

    @Override
    protected void onResume() {

        super.onResume();
        filterData();
    }
    }
4

3 回答 3

1

使用 AsyncTask 可能更容易http://developer.android.com/reference/android/os/AsyncTask.html

所以你会有这样的东西

public class Temp extends Activity {
    private Context mContext;
    private ProgressDialog mDialog

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

        setContentView(R.layout.temp);


        mDialog = ProgressDialog.show(this, "Filter", "blah", true, true);
        mDialog.show();
        new FillDataTask.execute();

    }


    class FillDataTask extends AsyncTask {
        @Override
        protected Object doInBackground(Object... params) {

             try {
                    filterData();
                } catch (ParseException e) {
                    Log.e("FILTER", e.toString());
                    e.printStackTrace();
                }
            return true;  // you can return whatever you want here
        }

        protected void onPostExecute(Object result) {
            //update progressDialog here
            mDialog.dismiss();
        }
    }

}

在下面编辑

像这样做

public class CalendarMonth extends Activity {

    private ProgressDialog mDialog;

        private int mMonth = 0;
    private int mYear = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calendar_m);

        filterData();
    }

    private void filterData() {

        mDialog = ProgressDialog.show(this, "Loading calendar", 
                "Please wait...", true, true);
        mDialog.setCancelable(false);
        mDialog.show();

        new GenerateMonth().execute();
    }

    private void genMonth()
    {

        mCalendar = new CalendarInstance(mMonth, mYear);

        mMonth = this.mCalendar.getSelectedMonth();
        mYear = this.mCalendar.getSelectedYear();

    }

    private void layoutUI()
    {
          TextView monthName = (TextView) findViewById(R.id.monthName);
        monthName.setText(mCalendar.getMonthAndYearAsString());

        Button monthPrevious = (Button) findViewById(R.id.monthPrevious);
        monthPrevious.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                int month = mMonth - 1;
                int year = mYear;
                if (mMonth == 1) {
                    month = 12;
                    year = mYear - 1;
                }
                mMonth = month;
                mYear = year;
                filterData();
            }
        });

        Button monthNext = (Button) findViewById(R.id.monthNext);
        monthNext.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                int month = mMonth + 1;
                int year = mYear;
                if (mMonth == 12) {
                    month = 1;
                    year = mYear + 1;
                }
                mMonth = month;
                mYear = year;
                filterData();
            }
        });

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setNumColumns(7);
        gridview.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
        gridview.setBackgroundColor(Color.BLACK);
        gridview.setAdapter(new CalendarMonthAdapter(this, mMonth, mYear)); 
    }
}
}

class GenerateMonth extends AsyncTask {
        protected Object doInBackground(Object... arg0) {
            genMonth();
            return true;
        }
        protected void onPostExecute(Object result) {
            layoutUI();
            mDialog.dismiss();

        }

    }
于 2010-12-16T19:15:18.407 回答
0

错误必须在 filterData 中,如果您从过滤器数据更新 UI,它将给出该异常。我建议使用异步任务,使用 doInBackground 来构建数据,并使用 onPostExecute 来更新 UI。

于 2010-12-16T19:12:47.007 回答
0

看来您的问题出在 generateMonth 函数中,该函数是从 doInBackground 函数调用的。您不应从 doInBackground 函数或从 doInBackground 调用的任何函数访问 UI。此功能旨在执行需要大量时间的任务,例如计算或复杂的数据库查询,但不得访问 UI。您的 UI 更新应该在函数 onPostExecute 中完成。所以,像:

gridview.setNumColumns(7)

在 doInBackground (或从那里调用的 generateMonth )中是不允许的。

于 2010-12-16T21:19:59.453 回答