进度对话框在android中获取错误

时间:2014-12-08 10:05:05

标签: android android-asynctask android-progressbar

我有asynctask类,我尝试发送http request.i也使用进度对话框,但我无法显示进度对话框。我有错误 这是我的来源

private class SendAccesssTokenToServer extends
        AsyncTask<String, Void, String> {
    ProgressDialog pDialog;

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

        pDialog = new ProgressDialog(getActivity());

        pDialog.setCancelable(false);
        pDialog.show();
        pDialog.setContentView(R.layout.custom_progressdialog);
    }

    @Override
    protected String doInBackground(String... params) {
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        if (pDialog != null) {
            pDialog.dismiss();

            pDialog = null;
        }

        Log.e("Result issss", result + "res");

    }
}

这是我的逻辑错误 enter image description here

我怎么能解决我的问题?如果有人知道解决方案,请帮助我 感谢

3 个答案:

答案 0 :(得分:0)

您错误地放置了代码。检查以下正确的代码:

private class SendAccesssTokenToServer extends
    AsyncTask<String, Void, String> {
ProgressDialog pDialog;

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

    pDialog = new ProgressDialog(getActivity());
    pDialog.setContentView(R.layout.custom_progressdialog);
    pDialog.setCancelable(false);
    pDialog.show();

}

@Override
protected String doInBackground(String... params) {
    return null;
}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);

    if (pDialog != null) {
        pDialog.dismiss();
    }

 }
}

答案 1 :(得分:0)

@Override
protected void onPreExecute() 
{

    super.onPreExecute();

    pDialog = new ProgressDialog(LoginActivity.this);

    pDialog.setMessage("Loading car. Please wait...");

    pDialog.setIndeterminate(false);

    pDialog.setCancelable(false);

    pDialog.show();
}

protected void onPostExecute(String file_url) 
{
     // dismiss the dialog after getting values

     pDialog.dismiss();

}

答案 2 :(得分:0)

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

    pDialog = new ProgressDialog(getActivity());

    pDialog.setCancelable(false);
    pDialog.show();
    pDialog.setContentView(R.layout.custom_progressdialog);
}

首先显示progressDialog然后分配另一个布局而不关闭先前的progressDialog。这就是为什么你得到那个错误。

只需更改代码: -

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

    pDialog = new ProgressDialog(getActivity());
    pDialog.setContentView(R.layout.custom_progressdialog);
    pDialog.setCancelable(false);
    pDialog.show();

}

希望这会对你有所帮助:)。