安卓:进度对话框显示黑屏

3
这是我使用的代码,用于在相机触发和拍摄照片时实现一个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);

在我的安卓设备上点击保存按钮时,它没有显示进度对话框,而是出现了黑屏。请问可能出了什么问题?

请澄清一下。是整个屏幕变黑,还是只有出现的对话框是黑色的? - Knossos
你为对话框设置了自定义样式或主题吗? - Ramesh
尝试在活动中使用@android:style/Theme.Translucent.NoTitleBar主题。 - sud
整个屏幕变黑 - Blaze
@Sud:你能不能就上面的评论多解释一下?我觉得它看起来有点模糊。 - Blaze
2个回答

0

在编写进度对话框时,请记住它应该在单独的线程中运行。如果在 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()在不同的线程上运行,这也是AsyncTask设计的主要原因。

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


您的解决方案很完美,但我遇到了这个错误。 - Blaze
由于:java.lang.RuntimeException: 无法在未调用Looper.prepare()的线程中创建处理程序。 - Blaze
1
我将在发布完整代码时提出一个新的工单。 - Blaze

0

对我来说,只使用您的ProgressDialog代码行就可以工作,因此问题必须在其他地方,但您可以尝试:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接