好的,如果有人可以回答,我在这里有一个奇怪的问题。
我有一个应用程序可以启动默认相机拍照并将其上传到服务器。上传过程发生在扩展 AsyncTask 的内部类中。在调用异步任务之前,我有一个显示进度对话框的方法,然后在 AsyncTask 的 onPost Execute 方法中将其关闭。这一切都在我自己的设备上运行良好。当我在三星 Galaxy S3 上进行测试时,问题就出现了。
我在 progressDialog.Show(); 上得到一个空指针异常;它不会使应用程序崩溃,它只是不显示对话框。但是当我尝试关闭关于 PostExecute 的对话框时,我确实遇到了崩溃。我认为这是因为 S3 的相机似乎只在横向打开(尽管我的整个应用程序以 Google 可以告诉我的各种方式锁定为纵向)并且方向更改导致活动刷新导致引用progressDialog 将丢失。
对话框实例化的位置没有任何改变(onPreExecute() 确实有效,但我仍然遇到同样的问题),检查它是否为空或保存实例状态已经解决了这个问题。我在另一篇文章中读到,有些人在保存到 SD 卡时遇到了问题。S3 没有 SD 卡,但应用程序正在尝试写入外部存储。我尝试检查是否有 SD 卡在使用。
Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
即使没有 SD 卡,这实际上也返回了 true。将图像写入外部存储并不重要,因此我尝试使用它获取缓存副本。
File image = File.createTempFile(
imageFileName,
JPEG_FILE_SUFFIX,
this.getCacheDir()
);
这也没什么区别。为什么你问我是否关心这个 SD 卡问题,它不可能影响进度对话框。好吧,如果我将 SD 卡放入 S3,问题就会消失,并且进度对话框会完美显示。我不知道为什么会这样,但我不能保证使用该应用程序的人在他们的设备中会有 SD 卡,所以我需要找到一个合适的修复程序。
很抱歉这篇文章,但我认为它需要一些解释才能清楚地理解。任何想法或解决方案将不胜感激。
下面的代码是我正在使用的精简版本,我试图仅包含相关材料。
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.compare_layout_activity);
findViewById(R.id.bCompare).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try{
capturedImage = createImageFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(isNetworkAvailable()){
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(capturedImage));
startActivityForResult(i, REQ_CODE_PHOTO_TAKE);
}
else{
displayAlert("Network Error", "You do not have access to the internet. Please turn on your WiFi.");
}
}
});
}
protected File createImageFile() throws IOException {
// Create an image file name
String timeStamp =
new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = JPEG_FILE_PREFIX + timeStamp + "_";
File image = File.createTempFile(
imageFileName,
JPEG_FILE_SUFFIX,
this.getCacheDir()
);
capturedImagePath = image.getAbsolutePath();
return image;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
showProgressDialog();
new LongOperation(getApplicationContext()).execute("");
} else if (resultCode == Activity.RESULT_CANCELED) {
}
}
private void showProgressDialog(){
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Checking progress");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
}
//I wont include the full async task because the code executes fine except for the crash on the show progress Dialog so i'll include the onPostExecute
@Override
protected void onPostExecute(String result) {
if (progressDialog != null) {
progressDialog.dismiss();
}
几乎忘记包含堆栈跟踪,尽管它不是最有用的信息。这发生在 progressDialog.show();
02-27 18:11:33.945: E/WindowManager(21994): Activity com.activity.CompareActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41ed6318 that was originally added here
02-27 18:11:33.945: E/WindowManager(21994): android.view.WindowLeaked: Activity com.activity.CompareActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41ed6318 that was originally added here
02-27 18:11:33.945: E/WindowManager(21994): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:402)
02-27 18:11:33.945: E/WindowManager(21994): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:311)
02-27 18:11:33.945: E/WindowManager(21994): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
02-27 18:11:33.945: E/WindowManager(21994): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
02-27 18:11:33.945: E/WindowManager(21994): at android.view.Window$LocalWindowManager.addView(Window.java:558)
02-27 18:11:33.945: E/WindowManager(21994): at android.app.Dialog.show(Dialog.java:277)
02-27 18:11:33.945: E/WindowManager(21994): at com.activity.CompareActivity.showProgressDialog(CompareActivity.java:324)
02-27 18:11:33.945: E/WindowManager(21994): at com.activity.CompareActivity.onActivityResult(CompareActivity.java:249)
02-27 18:11:33.945: E/WindowManager(21994): at android.app.Activity.dispatchActivityResult(Activity.java:5368)
02-27 18:11:33.945: E/WindowManager(21994): at android.app.ActivityThread.deliverResults(ActivityThread.java:3178)
02-27 18:11:33.945: E/WindowManager(21994): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2603)
02-27 18:11:33.945: E/WindowManager(21994): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2644)
02-27 18:11:33.945: E/WindowManager(21994): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2130)
02-27 18:11:33.945: E/WindowManager(21994): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3553)
02-27 18:11:33.945: E/WindowManager(21994): at android.app.ActivityThread.access$700(ActivityThread.java:140)
02-27 18:11:33.945: E/WindowManager(21994): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1233)
02-27 18:11:33.945: E/WindowManager(21994): at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 18:11:33.945: E/WindowManager(21994): at android.os.Looper.loop(Looper.java:137)
02-27 18:11:33.945: E/WindowManager(21994): at android.app.ActivityThread.main(ActivityThread.java:4898)
02-27 18:11:33.945: E/WindowManager(21994): at java.lang.reflect.Method.invokeNative(Native Method)
02-27 18:11:33.945: E/WindowManager(21994): at java.lang.reflect.Method.invoke(Method.java:511)
02-27 18:11:33.945: E/WindowManager(21994): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
02-27 18:11:33.945: E/WindowManager(21994): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
02-27 18:11:33.945: E/WindowManager(21994): at dalvik.system.NativeStart.main(Native Method)