カスタムレイアウトtoast表示時にjava.lang.IllegalArgumentException: View not attached to window manager

887 views
Skip to first unread message

vacaposi

unread,
Aug 14, 2011, 11:21:15 PM8/14/11
to 日本Androidの会
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が非常に怪しいのですが、特に不審な点は
見つかりません。


以上になります。よろしくお願いいたします。

しかじろう

unread,
Aug 16, 2011, 12:56:20 PM8/16/11
to android-g...@googlegroups.com
しかじろうです。

View toastView = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.custom_toast));


View toastView = inflater.inflate(R.layout.custom_toast, null);

で動かないかなぁ?

あと、eclipseのRファイルが更新されてない時がたまにあるのでmenu -> project -> cleanをされてみてください。

2011/8/15 vacaposi <oh.no...@gmail.com>:

> --
> このメールは Google グループのグループ「日本Androidの会」の登録者に送られています。
> このグループに投稿するには、android-g...@googlegroups.com にメールを送信してください。
> このグループから退会するには、android-group-j...@googlegroups.com にメールを送信してください。
> 詳細については、http://groups.google.com/group/android-group-japan?hl=ja からこのグループにアクセスしてください。
>
>

--
/**
* @name : しかじろう【渡辺 慎二郎】
* @twitter : http://twitter.com/shikajiro
* @mail : shik...@gmail.com
* @profile http://www.google.com/profiles/shikajiro
*/

vacaposi

unread,
Aug 21, 2011, 11:14:36 AM8/21/11
to 日本Androidの会
しかじろう様、ご返信まことにありがとうございます。

先ほど、アドバイスいただきました方法(※1)により、再実行してみましたのですが、
状況変わらずでした。(※2)

以上になります。よろしくお願いいたします。


※1
View toastView = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.custom_toast));

View toastView = inflater.inflate(R.layout.custom_toast, null);

後に、menu -> project -> cleanを行い、実行。

※2
08-21 15:02:46.322: WARN/dalvikvm(350): threadid=1: thread exiting
with uncaught exception (group=0x4001d800)
08-21 15:02:46.342: ERROR/AndroidRuntime(350): FATAL EXCEPTION: main
08-21 15:02:46.342: ERROR/AndroidRuntime(350):
java.lang.IllegalArgumentException: View not attached to window
manager
08-21 15:02:46.342: ERROR/AndroidRuntime(350): at
android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:
355)
08-21 15:02:46.342: ERROR/AndroidRuntime(350): at
android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:200)
08-21 15:02:46.342: ERROR/AndroidRuntime(350): at
android.widget.Toast$TN.handleShow(Toast.java:370)
08-21 15:02:46.342: ERROR/AndroidRuntime(350): at
android.widget.Toast$TN$1.run(Toast.java:302)
08-21 15:02:46.342: ERROR/AndroidRuntime(350): at
android.os.Handler.handleCallback(Handler.java:587)
08-21 15:02:46.342: ERROR/AndroidRuntime(350): at
android.os.Handler.dispatchMessage(Handler.java:92)
08-21 15:02:46.342: ERROR/AndroidRuntime(350): at
android.os.Looper.loop(Looper.java:123)
08-21 15:02:46.342: ERROR/AndroidRuntime(350): at
android.app.ActivityThread.main(ActivityThread.java:4627)
08-21 15:02:46.342: ERROR/AndroidRuntime(350): at
java.lang.reflect.Method.invokeNative(Native Method)
08-21 15:02:46.342: ERROR/AndroidRuntime(350): at
java.lang.reflect.Method.invoke(Method.java:521)
08-21 15:02:46.342: ERROR/AndroidRuntime(350): at
com.android.internal.os.ZygoteInit
$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-21 15:02:46.342: ERROR/AndroidRuntime(350): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-21 15:02:46.342: ERROR/AndroidRuntime(350): at
dalvik.system.NativeStart.main(Native Method)
08-21 15:02:46.362: WARN/ActivityManager(60): Force finishing
activity jp.co.vacaposi.android.iv.iv.iv0010/.SdRead
08-21 15:02:46.882: WARN/ActivityManager(60): Activity pause timeout
for HistoryRecord{43ffd6c0
jp.co.vacaposi.android.iv.iv.iv0010/.SdRead}
08-21 15:02:47.982: INFO/Process(350): Sending signal. PID: 350 SIG: 9
08-21 15:02:48.012: INFO/ActivityManager(60): Process
jp.co.vacaposi.android.iv.iv.iv0010 (pid 350) has died.
08-21 15:02:48.012: INFO/WindowManager(60): WIN DEATH: Window{44008b48
jp.co.vacaposi.android.iv.iv.iv0010/
jp.co.vacaposi.android.iv.iv.iv0010.SdRead paused=false}
08-21 15:02:48.062: WARN/InputManagerService(60): Got RemoteException
sending setActive(false) notification to pid 350 uid 10036




On Aug 17, 1:56 am, しかじろう <shikaj...@gmail.com> wrote:
> しかじろうです。
>
> View toastView = inflater.inflate(R.layout.custom_toast,
> (ViewGroup) findViewById(R.id.custom_toast));
> ↓
> View toastView = inflater.inflate(R.layout.custom_toast, null);
>
> で動かないかなぁ?
>
> あと、eclipseのRファイルが更新されてない時がたまにあるのでmenu -> project -> cleanをされてみてください。
>
> 2011/8/15 vacaposi <oh.no.je...@gmail.com>:
> ...
>
> read more ≫- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages