繁体   English   中英

Android:进度对话框显示黑屏

[英]Android: Progress Dialog shows a black screen

这是我使用的代码,用于在相机触发和拍照时实现ProgressDialog。

public void onPhotoTaken() {

        _taken = true;

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 4;

        Bitmap bitmap = BitmapFactory.decodeFile(_path, options);

        try {
            ProgressDialog dialog = ProgressDialog.show(this, "Loading", "Please wait...", true);
            ExifInterface exif = new ExifInterface(_path);
            int exifOrientation = exif.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);

单击我的Android设备的保存按钮,而不是显示进度对话框,它显示黑屏。 请问有什么不对。

您应该记住,在进度对话框中,您应该在单独的线程中运行它。 如果您在UI线程中运行它,您将看不到任何对话框。

如果您不熟悉 Android线程,那么您应该了解AsyncTask 这有助于您实现无痛的线程

示例代码:

private class CheckTypesTask extends AsyncTask<Void, Void, Void>{
        ProgressDialog asyncDialog = new ProgressDialog(IncidentFormActivity.this);
        String typeStatus;


        @Override
        protected void onPreExecute() {
            //set message of the dialog
            asyncDialog.setMessage(getString(R.string.loadingtype));
            //show dialog
            asyncDialog.show();
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... arg0) {

            //don't touch dialog here it'll break the application
            //do some lengthy stuff like calling login webservice

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            //hide the dialog
            asyncDialog.dismiss();

            super.onPostExecute(result);
        }

}

祝好运。

编辑

方法show()必须从用户界面(UI)线程doInBackground() ,而doInBackground()在不同的线程上运行,这是AsyncTask设计的主要原因。

您必须在onProgressUpdate()onPostExecute调用show()

它只是使用您的ProgressDialog代码行,因此问题必须在其他地方,但您可以尝试:

ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Please wait...");
dialog.setTitle("Loading");
dialog.show();

暂无
暂无

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

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