简体   繁体   English

警报对话框未显示在片段内

[英]Alert Dialog not being shown inside fragment

I have looked at numerous other threads regarding my problem and still can't find the answer.我已经查看了许多关于我的问题的其他线程,但仍然找不到答案。 My alert dialog for some reason isn't being shown inside my fragment.由于某种原因,我的警报对话框没有显示在我的片段中。 Using the same code and modifying slightly, works fine inside an activity.使用相同的代码并稍作修改,在活动中工作正常。 Can anyone spot my mistake?谁能发现我的错误?

final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final LayoutInflater layoutInflater = (LayoutInflater) getActivity().getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);

View view = layoutInflater.inflate(R.layout.popup_update, null);
builder.setView(view);

builder.create();
TextView txtMessage = view.findViewById(R.id.txtMessage);
txtMessage.setText("SAVING");

final Dialog popupDialog = builder.show();
popupDialog.setCancelable(false);
popupDialog.setCanceledOnTouchOutside(false);

Change to改成

final LayoutInflater layoutInflater = getActivity().getLayoutInflator();
View view = layoutInflater.inflate(R.layout.popup_update, null);

AlertDialog alert = builder.create();
alert.show();
LayoutInflater layoutInflater = (LayoutInflater) getActivity().getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.popup_update, null);
TextView txtMessage = view.findViewById(R.id.txtMessage);
txtMessage.setText("SAVING");

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
.setView(view)
.setCancelable(false)
.setCanceledOnTouchOutside(false);

AlertDialog dialog = builder.create();
dialog.show();
View view = getActivity().getLayoutInflater().inflate(R.layout.popup_update, null);
            final Dialog popupDialog = new AlertDialog.Builder(getActivity())
                    .setView(view)
                    .setCancelable(false)
                    .create();
            TextView txtMessage = view.findViewById(R.id.txtMessage);
            txtMessage.setText("SAVING");
            popupDialog.show();

I solved my issue by using an Async task我通过使用异步任务解决了我的问题

class SaveTrainingTask extends AsyncTask<Void, Boolean, Boolean> {
        Dialog popupDialog;

        public SaveTrainingTask() {
            final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            final LayoutInflater layoutInflater = (LayoutInflater) getActivity().getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);

            View view = layoutInflater.inflate(R.layout.popup_update, null);
            TextView txtMessage = view.findViewById(R.id.txtMessage);
            txtMessage.setText("SAVING TRAINING");
            builder.setView(view);

            builder.create();

            popupDialog = builder.show();
            popupDialog.setCancelable(false);
            popupDialog.setCanceledOnTouchOutside(false);
        }

        protected Boolean doInBackground(Void... urls) {
            saveInspection();
            return true;
        }

        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);

            popupDialog.dismiss();

            if (result) {
                Toast.makeText(getContext(), "Successfully saved training.", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getContext(), SelectEquipmentActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                getActivity().finish();
            } else {
                Toast.makeText(getContext(), "Error saving training.", Toast.LENGTH_SHORT).show();
            }
        }
    }

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

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