Dialog不通过AsyncTask显示

时间:2017-06-07 23:15:51

标签: android android-asynctask

我有一个自定义进度对话框,除了在我的代码中,它似乎无处不在:

我的登录活动(相关摘要):

@Override
protected void onCreate(Bundle savedInstanceState) {

    try {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        context = this;
        pd = AUtils.getProgressDialog(context, false);


                UserExistsAuthenticateAndRoute = getIntent().getBooleanExtra("UserExistsAuthenticateAndRoute", false);
        RouteToActivity = getIntent().getStringExtra("RouteToActivity");

        //make sure there is no token in APrefs in memory during login
        APrefs pref = new APrefs();
        if (pref != null) {
            pref.putNMToken(null);
            pref.putNMRefreshToken(null);
        }


        ClickableSpan span = new ClickableSpan() {

            @Override
            public void onClick(View widget) {

            }
        };

        setActionBar();
        initUi();
        mToolbarTitle.setText("Log In");
    } catch (Exception exc) {
        exc.printStackTrace();
    }

}
        @Override
        protected Void doInBackground(Void... voids) {


            GDDataManager.get().login(GDUser, new DataCallBack() {
                        @Override
                        public void update(DataUpdate update) {

                            if (update.code == 0) {
                                final GDUser _gdUser = pref.getMember();
                                //call get status
                                if (_gdUser != null) {
                                    Log.i(TAG, "getUserStatus()");
                                    GDDataManager.get().getUserStatus(_gdUser, new DataCallBack() {
                                        @Override
                                        public void update(DataUpdate update) {

                                            if (update.code == 0) {
                                                setGdUserStatus((GDUserStatus) update.data);
                                                loginController(getGdUserStatus(), _gdUser);

                                            } else {

                                                Log.e(TAG, "getUserStatus(), error response msg " + update.message);
                                                if (update.message.contains("error")) {
                                                    App.toast(getString(R.string.general_server_error_message));

                                                }


                                            }
                                        }
                                    });
                                }

                            } else {

                                Log.e(TAG, "update message:" + update.message);
                                if (update.message.contains("error")) {
                                    App.toast(getString(R.string.general_server_error_message));
                                } else if (update.message.contains("could not verify password")) {
                                    App.toast(getString(R.string.could_not_verify_password));
                                } else if (update.message.contains("no user found")) {
                                    App.toast(getString(R.string.no_user_found));
                                } else {
                                    App.toast(update.message);
                                }

                                if (btnLogIn != null) {
                                    //disable is valid in order to prevent double click
                                    btnLogIn.setEnabled(false);
                                    btnLogIn.setTextColor(ContextCompat.getColor(context, R.color.colorGrey));
                                }

                                edtEmail.setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(context, R.drawable.cross_icon), null);
                                edtEmail.setBackground(ContextCompat.getDrawable(context, R.drawable.textfield_red));
                                edtPassword.setBackground(ContextCompat.getDrawable(context, R.drawable.textfield_red));
                            }

                        }//end update getUserStatus
                    }

            );

            return null;
        }
    };

    try {
        tryLoginTask.execute();
    } catch (Exception exc) {
        Log.d(TAG, exc.getMessage());
        exc.printStackTrace();
        //cancel task on exception , DISMISS DIALOG to avoid locking screen 
        tryLoginTask.cancel(true);
    }


}//end tryLogin()

实用程序类的静态代码是返回对话框(相关代码段):

public static Dialog getProgressDialog(Context c, boolean isCancelable) {

    Dialog pd = new Dialog(c,c.getApplicationInfo().theme);
    pd.setCanceledOnTouchOutside(isCancelable);
    pd.requestWindowFeature (Window.FEATURE_NO_TITLE);
    pd.setContentView (R.layout.progress_dialog);
    pd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.argb(150,0,0,0)));

    return pd;
}

我没有看到任何错误,异常,并且使用相同的方法在其他地方显示对话框。有时我会看到它只有一秒钟但是任务还没有完成。

任何建议。

由于

1 个答案:

答案 0 :(得分:0)

AlertDialogs是前台事物。您应该在UI线程中显示对话框。因此,如果要在AsyncTask中显示对话框,则应使用runOnUiThread

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        // Show your dialog here
    }
});

之证件: