2015-11-23 50 views
3

这是我用来在照相机开启并拍摄照片时用于实现ProgressDialog的代码。Android:进度对话框显示黑色屏幕

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设备的保存按钮,而不是显示进度对话框,它会显示一个黑屏。请问可能是错的。

+0

请澄清。整个屏幕变黑吗?或者它是出现黑色的对话框。 – Knossos

+0

你有没有给出对话框的任何自定义样式/主题 – Ramesh

+0

'@android:style/Theme.Translucent.NoTitleBar'在活动中尝试此主题 – sud

回答

0

你应该记住当谈到Progress dialog是你应该在一个单独的线程运行它。如果你在UI线程中运行它,你将看不到对话框。

如果您是Android Threading的新手,那么您应该了解AsyncTask。这有助于你实现painless Threads

示例代码:

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

} 

好运。

EDIT

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

你必须调用show()无论是在onProgressUpdate()onPostExecute

+0

你的解决方案是完美的,但我有这个错误 – Blaze

+0

引起:java.lang.RuntimeException:不能创建处理程序内部线程没有调用Looper.prepare() – Blaze

+0

查看编辑答案 –

0

这是为我工作只是使用的代码你ProgressDialog线,所以这个问题必须在其他地方,但你可以尝试:

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

结果相同。仍然显示黑屏 – Blaze

相关问题