简体   繁体   English

进度对话框未显示

[英]Progress Dialog is not showing up

Used following code for showing progress dialog, 使用以下代码显示进度对话框,

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) {

    }
}

Calling showDialog in onCreate of Activity class 在Activity类的onCreate中调用showDialog

@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();
    }

}

On debugging, showDaialog method is executed(dialog is not visible though) completely but when app is running it doesn't execute showDialog as I cannot see my Log statements in Android monitor 在调试时, showDaialog执行了showDaialog方法(虽然看不到对话框),但是在应用程序运行时它不会执行showDialog因为我无法在Android监视器中看到我的Log语句

Try this 尝试这个

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) {

    }
}

also remove this line from your oncreate. 还要从oncreate中删除此行。

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

Remove below line or give some time delay to hide progressDialog,Because showDialog() is showing dialog and suddenly it closed, 删除下面的行或稍稍延迟以隐藏progressDialog,因为showDialog()显示对话框并突然关闭,

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

I have made some changes in your code. 我对您的代码进行了一些更改。

  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) {

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM