执行.show()方法时,警报不会立即显示

时间:2018-01-28 00:05:39

标签: java android alertdialog

我正在构建并显示AlertDialog,但是当调用alert.show()方法时,警报不会出现(在下面的代码的第15行)。它出现在逻辑中的后期 - 在下面的代码示例结束之后。 此代码来自Activity类(扩展ActionBarActivity),并在单击关联布局上的按钮时执行。

为什么在第16行执行alert.show()方法后,我的警报没有立即显示?

public void onClickChooseFolder(final View v) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Confirm");
    builder.setMessage("Is your folder on an external SD card?");
    builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // Do something here
        }
    });
    builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // Do something here
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
    int permission = ContextCompat.checkSelfPermission(this,
            Manifest.permission.READ_EXTERNAL_STORAGE);
    if (permission != PackageManager.PERMISSION_GRANTED) {
        Log.i(TAG, "Permission to read external storage denied");
    }
    permission = ContextCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (permission != PackageManager.PERMISSION_GRANTED) {
        Log.i(TAG, "Permission to write external storage denied");
    }
    String m_chosenDir = "";
    Boolean m_newFolderEnabled = false;
    // Make the folder list ListView visible
    DirectoryChooserDialog directoryChooserDialog =
            new DirectoryChooserDialog(SourceFolders.this,
                    new DirectoryChooserDialog.ChosenDirectoryListener()
                    {
                        @Override
                        public void onChosenDir(String chosenDir)
                        {
                            String sourceFolder = chosenDir;
                            //Check whether the chosen folder is empty or invalid
                            File f = new File(sourceFolder);
                            File file[] = f.listFiles();
                            //If file is null then the source folder isn't valid
                            if (file == null) {
                                Toast.makeText(SourceFolders.this, "Selected folder is not valid",
                                        Toast.LENGTH_LONG).show();
                                return;
                            }
                            int fileCount = file.length;
                            if (fileCount < 1) {
                                Toast.makeText(SourceFolders.this, "Selected folder is empty",
                                        Toast.LENGTH_LONG).show();
                            }
                            // Add the folder to the folder list
                            // If this is not the paid version of the app then clear the folder list before adding this to it
                            // (Only one folder allowed in free version)
                            if (Utilities.upLoadFiles(SourceFolders.this) < 10) {
                                folderlist.clear();
                            }
                            //save the folder list to source_folder preference
                            folderlist.add(sourceFolder);
                            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SourceFolders.this);
                            SharedPreferences.Editor edit = prefs.edit();
                            Set<String> set = new HashSet<String>();
                            set.addAll(folderlist);
                            edit.putStringSet("source_folders", set);
                            edit.commit();
                            refreshListViewFromPrefs();
                            setListViewOnClickListener();
                            String fileName;
                            int imageCount = 0;
                            int videoCount = 0;
                            for (int i = 0; i < file.length; i++) {
                                fileName = file[i].getName();
                                if (fileName.endsWith("jpg") || fileName.endsWith("JPG")|| fileName.endsWith("jpeg")|| fileName.endsWith("JPEG")) {
                                    imageCount++;
                                }
                                if (fileName.endsWith("mp4") || fileName.endsWith("MP4")|| fileName.endsWith("avi")|| fileName.endsWith("AVI")) {
                                    videoCount++;
                                }
                            }
                            if (imageCount>0 || videoCount>0) {
                                Toast.makeText(SourceFolders.this, "Selected folder contains " + imageCount + " photo(s) and " + videoCount + " video(s)",
                                        Toast.LENGTH_LONG).show();
                                return;
                            } else {
                                Toast.makeText(SourceFolders.this, "Selected folder contains no photos or videos",
                                        Toast.LENGTH_LONG).show();
                                return;
                            }
                        }
                    });
    directoryChooserDialog.setNewFolderEnabled(m_newFolderEnabled);
    directoryChooserDialog.chooseDirectory(m_chosenDir);
    m_newFolderEnabled = ! m_newFolderEnabled;
}

0 个答案:

没有答案