vacaposiと申します。よろしくお願いいたします。
カスタムレイアウトのtoastを表示したときに下記のエラーが発生いたします。
(アプリの実行はできるのですが、toastを表示しようとすると実行時エラーが発生します。)
何が原因でエラーが発生しているのか色々試したのですが、どこでエラーが発生しているのか
までしか分かりませんでした。アドバイスよろしくお願いいたします。
【エラーメッセージ】
08-15 02:34:42.083: WARN/dalvikvm(270): threadid=3: thread exiting
with uncaught exception (group=0x4001b188)
08-15 02:34:42.083: ERROR/AndroidRuntime(270): Uncaught handler:
thread main exiting due to uncaught exception
08-15 02:34:42.083: ERROR/AndroidRuntime(270):
java.lang.IllegalArgumentException: View not attached to window
manager
08-15 02:34:42.083: ERROR/AndroidRuntime(270): at
android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:
355)
08-15 02:34:42.083: ERROR/AndroidRuntime(270): at
android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:200)
08-15 02:34:42.083: ERROR/AndroidRuntime(270): at
android.widget.Toast$TN.handleShow(Toast.java:370)
08-15 02:34:42.083: ERROR/AndroidRuntime(270): at
android.widget.Toast$TN$1.run(Toast.java:302)
08-15 02:34:42.083: ERROR/AndroidRuntime(270): at
android.os.Handler.handleCallback(Handler.java:587)
08-15 02:34:42.083: ERROR/AndroidRuntime(270): at
android.os.Handler.dispatchMessage(Handler.java:92)
08-15 02:34:42.083: ERROR/AndroidRuntime(270): at
android.os.Looper.loop(Looper.java:123)
08-15 02:34:42.083: ERROR/AndroidRuntime(270): at
android.app.ActivityThread.main(ActivityThread.java:4363)
08-15 02:34:42.083: ERROR/AndroidRuntime(270): at
java.lang.reflect.Method.invokeNative(Native Method)
08-15 02:34:42.083: ERROR/AndroidRuntime(270): at
java.lang.reflect.Method.invoke(Method.java:521)
08-15 02:34:42.083: ERROR/AndroidRuntime(270): at
com.android.internal.os.ZygoteInit
$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-15 02:34:42.083: ERROR/AndroidRuntime(270): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-15 02:34:42.083: ERROR/AndroidRuntime(270): at
dalvik.system.NativeStart.main(Native Method)
08-15 02:34:42.093: INFO/Process(53): Sending signal. PID: 270 SIG: 3
08-15 02:34:42.103: INFO/dalvikvm(270): threadid=7: reacting to signal
3
08-15 02:34:42.103: ERROR/dalvikvm(270): Unable to open stack trace
file '/data/anr/traces.txt': Permission denied
08-15 02:34:43.503: INFO/Process(270): Sending signal. PID: 270 SIG: 9
08-15 02:34:43.533: INFO/ActivityManager(53): Process
jp.co.vacaposi.android.iv.iv.iv0010 (pid 270) has died.
08-15 02:34:43.553: INFO/WindowManager(53): WIN DEATH: Window{43d03000
jp.co.vacaposi.android.iv.iv.iv0010/
jp.co.vacaposi.android.iv.iv.iv0010.SdRead paused=false}
08-15 02:34:43.593: INFO/UsageStats(53): Unexpected resume of
com.android.launcher while already resumed in
jp.co.vacaposi.android.iv.iv.iv0010
08-15 02:34:43.703: WARN/InputManagerService(53): Got RemoteException
sending setActive(false) notification to pid 270 uid 10024
08-15 02:34:43.733: ERROR/gralloc(53): [unregister] handle 0x4af348
still locked (state=40000001)
08-15 02:34:45.563: WARN/NotificationService(53): Object died trying
to hide notification android.app.ITransientNotification$Stub
$Proxy@43cd5e80 in package jp.co.vacaposi.android.iv.iv.iv0010
08-15 02:34:45.563: WARN/ActivityManager(53): setProcessForeground
called on unknown pid: 270
【ソース】
■SdRead.java
public class SdRead extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
…(画像取得処理)
setContentView(R.layout.main);
gridView = (GridView) findViewById(R.id.GridView01);
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
// 画像クリック時の動作を制御
public void onItemClick(AdapterView<?> parent, View view,int
position, long id) {
ToastMaster mToast = new ToastMaster(SdRead.this);
mToast.setDuration(Toast.LENGTH_LONG);
LayoutInflater inflater = getLayoutInflater();
View toastView = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.custom_toast));
ImageView imageView = (ImageView)
toastView.findViewById(R.id.toast_image);
//ImageView imageView = new ImageView(SdRead.this);
TextView imageName =
(TextView)toastView.findViewById(R.id.toast_name);
TextView imageDate =
(TextView)toastView.findViewById(R.id.toast_date);
// 画像リストにポジションを引数に画像ファイルオブジェクトを取得
File file = new File(imageList.get(position).getPath());
// ビットマップ作成オブジェクトの設定
BitmapFactory.Options bmfOptions = new BitmapFactory.Options();
// ARGBでそれぞれ0~127段階の色を使用(メモリ対策)
bmfOptions.inPreferredConfig = Config.ARGB_4444;
// 画像を1/2サイズに縮小(メモリ対策)
bmfOptions.inSampleSize = 2;
// システムメモリ上に再利用性の無いオブジェクトがある場合に勝手に解放(メモリ対策)
bmfOptions.inPurgeable = true;
// 現在の表示メトリクスの取得
DisplayMetrics dm =
SdRead.this.getResources().getDisplayMetrics();
// ビットマップのサイズを現在の表示メトリクスに合わせる(メモリ対策)
bmfOptions.inDensity = dm.densityDpi;
// 画像ファイルオブジェクトとビットマップ作成オブジェクトから、ビットマップオブジェクト作成
Bitmap bmImage =
BitmapFactory.decodeFile(file.getPath(),bmfOptions);
// ビットマップオブジェクトをImageViewオブジェクトにセット
imageView.setImageBitmap(bmImage);
imageName.setText("text");
imageDate.setText("date");
// トーストオブジェクトにImageViewオブジェクトをセット
mToast.setView(imageView);
// トースト表示前にガッベージコレクション
System.gc();
// トースト表示
mToast.show();
// オブジェクト参照解除
bmImage = null;
imageView = null;
// トースト表示後のガッベージコレクション
System.gc();
}
});
}
public class ImageAdapter extends BaseAdapter {
…
return imageView;
}
}
}
■custom_toast.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
http://schemas.android.com/apk/res/
android"
android:id="@+id/custom_toast"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:background="#DAAA"
>
<ImageView android:id="@+id/toast_image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
/>
<TextView android:id="@+id/toast_name"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"
/>
<TextView android:id="@+id/toast_date"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"
/>
</LinearLayout>
【エラーの発生箇所】
ImageView imageView = (ImageView)
toastView.findViewById(R.id.toast_image);
の箇所のようです。
//ImageView imageView = new ImageView(SdRead.this);
に変えて、画像のみの表示にするとうまく表示されます。
現象だけをみてみるとcustom_toast.xmlが非常に怪しいのですが、特に不審な点は
見つかりません。
以上になります。よろしくお願いいたします。