0

我在我的应用程序中使用 ProgressDialog,但在运行应用程序时发生错误。

这是代码

   public class MainActivity extends Activity{


    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

        ProgressDialog mDialog = new ProgressDialog(getApplicationContext());
        mDialog.setMessage("Loading...");
        mDialog.setCancelable(false);
        mDialog.show();

    }
}

更新代码:

公共类 MainActivity 扩展 Activity{

public static ProgressDialog dialog;


public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);



}


 private static abstract class DictionaryOpenHelper extends SQLiteOpenHelper {



     public DictionaryOpenHelper() {
        super(null, null, null, 0);
        // TODO Auto-generated constructor stub
    }



    public void onCreate() {
        // TODO Auto-generated method stub

        ProgressDialog dialog = ProgressDialog.show(DictionaryOpenHelper.this, "", 
                "Loading. Please wait...", true);  

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }


 }

}

并在 ProgressDialog 对话框上给出错误 = ProgressDialog.show(DictionaryOpenHelper.this, "", "Loading. Please wait...", true); :

ProgressDialog 类型中的方法 show(Context, CharSequence, CharSequence, boolean) 不适用于参数 (MainActivity.DictionaryOpenHelper, String, String, boolean)

4

6 回答 6

1

尝试

ProgressDialog dialog = ProgressDialog.show(yourActivity.this, "", 
                        "Loading. Please wait...", true);  
于 2013-01-18T06:36:17.813 回答
1
    Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
01-18 06:26:07.273: E/AndroidRuntime(3853):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:524)

可以使用以下代码段尝试加载和卸载进度对话框,使其成为 UIThread ///load 的一部分

  private ProgressDialog pDialogTh = null;
      private void showLoading() {
        runOnUiThread(new Runnable() {
            public void run() {
                // if(pDialog==null)
                pDialogTh = ProgressDialog.show(YourActivity.this, "", "Loading...",
                        true, true);
                pDialogTh.setCancelable(false);
                if (!pDialogTh.isShowing()) {
                    pDialogTh.show();
                }
            }
        });
    }

///////卸下

private void hideLoading() {
    runOnUiThread(new Runnable() {
        public void run() {
            if (pDialogTh.isShowing()) {
                pDialogTh.cancel();
            }
        }
    });
}
于 2013-01-18T06:40:45.017 回答
0

如果您正在使用选项卡并且想要进度对话框,那么...

public class MainActivity extends Activity{


public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

    ProgressDialog mDialog = new ProgressDialog(getParent());
    mDialog.setMessage("Loading...");
    mDialog.setCancelable(false);
    mDialog.show();

}
}

否则就写这个...

public class MainActivity extends Activity{


public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

    ProgressDialog mDialog = new ProgressDialog(MainActivity,this);
    mDialog.setMessage("Loading...");
    mDialog.setCancelable(false);
    mDialog.show();

}
}
于 2013-01-18T06:52:22.663 回答
0

我在我的异步任务上使用它。这就像魅力一样。

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            dialog = new ProgressDialog(ActivityAddTicket.this);
            dialog.setTitle(R.string.processing);
            dialog.setMessage(getResources().getString(R.string.loading));
            dialog.show();
        };
于 2014-04-15T06:32:02.027 回答
0

尝试 :

public class MainActivity extends Activity{


public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

    ProgressDialog mDialog = new ProgressDialog(MainActivity.this);
    mDialog.setMessage("Loading...");
    mDialog.setCancelable(false);
    mDialog.show();

}

}

于 2013-01-18T06:41:22.483 回答
0

嗨,将 getApplicationContext() 更改为您的活动名称

  ProgressDialog mDialog = new ProgressDialog(MainActivity.this);
于 2013-01-18T06:42:22.467 回答