片段内未显示警报对话框

时间:2019-08-12 09:31:14

标签: android android-alertdialog

我查看了有关我的问题的许多其他线索,但仍然找不到答案。由于某些原因,我的警报对话框未显示在片段中。使用相同的代码并稍加修改,即可在活动中正常运行。谁能发现我的错误?

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

4 个答案:

答案 0 :(得分:0)

更改为

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

AlertDialog alert = builder.create();
alert.show();

答案 1 :(得分:0)

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

答案 2 :(得分:0)

import pandas as pd
import nltk
nltk.download('punkt')

df = pd.DataFrame (
        {
        'student number' : [1,2,3,4,5],
        'answer' : [ 'Yes, she is correct.', 'Yes', 'no', 'north east', 'No its North East']
        # I know there's an apostrophe missing
        }
)       
print(df)

# change all text to lower case
df['answer'] = df['answer'].str.lower()

# split the answer into individual words
df['text'] = df['answer'].apply(nltk.word_tokenize)

# Check if given words appear together in a list of sentence 
def check(sentence, words): 
   res = [] 
   for substring in sentence: 
       k = [ w for w in words if w in substring ] 
       if (len(k) == len(words) ): 
            res.append(substring) 
   return res

# Driver code 
sentence = df['text'] 
words = ['no','north','east'] 
print(check(sentence, words))

答案 3 :(得分:0)

我通过使用异步任务解决了问题

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