进度对话框未显示

时间:2016-11-22 06:17:46

标签: android android-activity progressdialog

使用以下代码显示进度对话框

public void showDialog() {
    try {
        Log.d(TAG, "showDialog--------------");
        progressDialog = new Dialog(RewardsActivity.this);
        final View dialogView = LayoutInflater.from(RewardsActivity.this).inflate(R.layout.progress_dialog, null);
        progressDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        progressDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        progressDialog.setContentView(dialogView);

        progressDialog.setCancelable(true);

        final ImageView progressSpinner = (ImageView) dialogView.findViewById(R.id.ivProgress);

        final RotateAnimation anim = new RotateAnimation(0f, 350f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        anim.setRepeatCount(Animation.INFINITE);
        anim.setDuration(500);
        anim.setInterpolator(new LinearInterpolator());
        progressSpinner.startAnimation(anim);

        progressDialog.show();
        Log.d(TAG, "showDialog--------------");
        progressDialog.getWindow().setLayout(120, 120);
    } catch (Exception e) {

    }
}

在活动类的showDialog中调用onCreate

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

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    tinyDB = new TinyDB(getApplicationContext());
    showDialog();
    new GetRewardsResponse(getApplicationContext(), this, tinyDB);


    viewpager = (ViewPager) findViewById(R.id.viewPager);
    indicator = (CircleIndicator) findViewById(R.id.indicator);
    indicator.setViewPager(viewpager);
    if (progressDialog.isShowing()) {
        progressDialog.hide();
    }

}

在调试时,showDaialog方法已完全执行(虽然对话框不可见)但是当应用程序运行时它不会执行showDialog,因为我在Android监视器中看不到我的日志语句< / p>

3 个答案:

答案 0 :(得分:1)

试试这个

public void showDialog() {
    try {
        Log.d(TAG, "showDialog--------------");
        progressDialog = new Dialog(RewardsActivity.this);
        final View dialogView = LayoutInflater.from(RewardsActivity.this).inflate(R.layout.progress_dialog, null);
        progressDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        progressDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        progressDialog.setContentView(dialogView);

        progressDialog.setCancelable(true);

        final ImageView progressSpinner = (ImageView) dialogView.findViewById(R.id.ivProgress);

        final RotateAnimation anim = new RotateAnimation(0f, 350f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        anim.setRepeatCount(Animation.INFINITE);
        anim.setDuration(500);
        anim.setInterpolator(new LinearInterpolator());
        progressSpinner.startAnimation(anim);

        progressDialog.show();
        Log.d(TAG, "showDialog--------------");
    } catch (Exception e) {

    }
}

也会从你的oncreate中删除这一行。

 if (progressDialog.isShowing()) {
        progressDialog.hide();
    }

答案 1 :(得分:0)

删除以下行或延迟一段时间来隐藏progressDialog,因为showDialog()显示对话框并突然关闭,

    if (progressDialog.isShowing()) {
        progressDialog.hide();
    }

答案 2 :(得分:0)

我在代码中做了一些更改。

  private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rewards);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        tinyDB = new TinyDB(getApplicationContext());
        showDialog();
        new GetRewardsResponse(getApplicationContext(), this, tinyDB);
        viewpager = (ViewPager) findViewById(R.id.viewPager);
        indicator = (CircleIndicator) findViewById(R.id.indicator);
        indicator.setViewPager(viewpager);
        if (progressDialog.isShowing()) {
            progressDialog.hide();
        }
    }

    public void showDialog() {
        try {
            Log.d(TAG, "showDialog--------------");
            LayoutInflater inflater = getLayoutInflater();
            progressDialog = new Dialog(mContext);
            final View dialogView = inflater.inflate(R.layout.progress_dialog, null);
            progressDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            progressDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
            progressDialog.setContentView(dialogView);
            progressDialog.setCancelable(true);
            final ImageView progressSpinner = (ImageView) dialogView.findViewById(R.id.ivProgress);
            final RotateAnimation anim = new RotateAnimation(0f, 350f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            anim.setRepeatCount(Animation.INFINITE);
            anim.setDuration(500);
            anim.setInterpolator(new LinearInterpolator());
            progressSpinner.startAnimation(anim);
            progressDialog.getWindow().setLayout(120, 120);
            progressDialog.show();
            Log.d(TAG, "showDialog--------------");
        } catch (Exception e) {

        }
    }