【问题标题】:progress dialog inflate on top of layout onPreExecute()?进度对话框在布局onPreExecute()之上膨胀?
【发布时间】:2012-11-15 10:50:41
【问题描述】:

我的 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() 中。争论显示(this, "Working", "Retreiving Neaby Restaurants", true, true) arnt 允许在 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() 上的异步任务中进行这项工作,我不想在用户真正开始使用异步任务之前显示()。

【问题讨论】:

    标签: android android-asynctask android-activity progressdialog android-progressbar


    【解决方案1】:

    使用

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

    onPreExecute()

    this 实际上是活动的Context。一个重要的东西,你需要在未来经常使用。

    你也可以做一个Class level variable上下文activityContext

    然后像这样在onCreatesetContentView之后初始化它

    activityContext=this

    然后你也可以使用

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

    【讨论】:

    • 完美的答案和解释。不是我习惯在这个网站上看到的东西。谢谢!
    猜你喜欢
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多