誰かがそれに答えることができれば、私はここに奇妙なものがあります。
デフォルトのカメラを起動して写真を撮り、それをサーバーにアップロードするアプリケーションがあります。アップロードプロセスは、AsyncTaskを拡張する内部クラスで行われます。非同期タスクを呼び出す前に、進行状況ダイアログを表示するメソッドがあります。このダイアログは、AsyncTaskのonPostExecuteメソッドで閉じられます。これはすべて、自分のデバイスで正常に機能します。サムスンギャラクシーS3でテストすると問題が発生します。
progressDialog.Show();でnullポインター例外が発生します。これはアプリケーションをクラッシュさせません。単にダイアログを表示しません。しかし、PostExecuteのダイアログを閉じようとすると、クラッシュします。これは、S3のカメラが横向きでしか開いていないように見え(Googleが可能な限りすべての方法でアプリケーション全体が縦向きにロックされているにもかかわらず)、向きの変更によってアクティビティが更新され、 progressDialogが失われます。
ダイアログがインスタンス化される場所を変更する必要はありません(onPreExecute()は機能しますが、同じ問題が発生します)。nullかどうかを確認するか、インスタンスの状態を保存すると、この問題が修正されます。私は別の投稿で、SDカードに保存するときに問題が発生している人がいることを読みました。S3にはSDカードがありませんでしたが、アプリは外部ストレージに書き込もうとしていました。使用しているSDカードがあるかどうかを確認してみました。
Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
SDカードがなくても実際に真に戻った。画像が外部ストレージに書き込まれることは重要ではなかったので、これを使用してキャッシュされたコピーを取得してみました。
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)