发布
社区首页 >问答首页 >进度对话框未显示

进度对话框未显示
EN

Stack Overflow用户
提问于 2016-11-22 14:17:46
回答 3查看 922关注 0票数 0

用于显示进度对话框的以下代码,

代码语言:javascript
代码运行次数:0
复制
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) {

    }
}

在Activity类的onCreate中调用showDialog

代码语言:javascript
代码运行次数:0
复制
@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,因为我在安卓监视器中看不到我的日志语句

EN

回答 3

Stack Overflow用户

发布于 2016-11-22 14:28:19

尝尝这个

代码语言:javascript
代码运行次数:0
复制
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中删除此行。

代码语言:javascript
代码运行次数:0
复制
 if (progressDialog.isShowing()) {
        progressDialog.hide();
    }
票数 1
EN

Stack Overflow用户

发布于 2016-11-22 14:30:34

删除下面的代码行或延迟一些时间来隐藏progressDialog,因为showDialog()正在显示对话框,但它突然关闭了。

代码语言:javascript
代码运行次数:0
复制
    if (progressDialog.isShowing()) {
        progressDialog.hide();
    }
票数 0
EN

Stack Overflow用户

发布于 2016-11-22 14:34:16

我对你的代码做了一些修改。

代码语言:javascript
代码运行次数: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) {

        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40735023

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档