Question

I get the following "java.lang.IllegalArgumentException: View not attached to window manager"

this happens when the toast.show() is called.

Please any explanation? Thanks a lot!!!!

EDITED: I post all the code + the logcat

CODE - NOTE the error occurs when the toast.show() is invoked in the listener.

public class MainActivity extends Activity {
    private Gallery gal;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        String[] descrizione =    {"cani","boffo","gigo","belo","fyyfy","bogin","boginetti","zippe","ninne","cestino","cagnin    o","cucciolo",};
        ArrayList<Elementi> el = new ArrayList<Elementi>(10);
        int[] tabDrawables = new int[] {
                     R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img1,R.drawable.img4,R.drawable.    img5,R.drawable.img6,R.drawable.img7,R.drawable.img8,R.drawable.img9,R.drawable.img10};

        for (int numero =1; numero < 10;numero++) {
            ImageView imgm = new ImageView(this);
            imgm.setImageResource(tabDrawables[numero]);
            el.add(new Elementi(imgm,descrizione[numero])); 
            };  

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Gallery gal=(Gallery)findViewById(R.id.galleria);
        gal.setAdapter(new MyAdapter(el));
        gal.setOnItemClickListener(new MyListener(this));       

    }
public class MyAdapter extends ArrayAdapter<Elementi> {
        public MyAdapter(ArrayList<Elementi> list) {
        super(MainActivity.this, R.layout.row, list);}

    public View getView(int position, View convertView,ViewGroup parent) {
           View row= convertView;  
           if(row==null){
                LayoutInflater inflater = (LayoutInflater)     MainActivity.this.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
                row = inflater.inflate(R.layout.row, null);    
           }

                TextView txt =(TextView)row.findViewById(R.id.testorow);
                txt.setText(getItem(position).tx) ;
                ImageView ima =(ImageView)row.findViewById(R.id.immagine);
                ima.setImageDrawable(getItem(position).im.getDrawable());
                return row;};

}  


public class Elementi { 
public ImageView im;
public String tx;
public Elementi(ImageView img,String txt){
        im =img;
        tx = txt;       
    }
}

public class MyListener implements AdapterView.OnItemClickListener {
     private final Context mContext;
     public MyListener(Activity ctx) {
          mContext = ctx;
        }
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        Toast toast = new Toast(mContext);
        toast.setView(arg1);
        toast.show();

    }}

}

LOGCAT

11-30 17:26:35.421: W/Trace(765): Unexpected value from nativeGetEnabledTags: 0
11-30 17:26:35.440: W/Trace(765): Unexpected value from nativeGetEnabledTags: 0
11-30 17:26:35.440: W/Trace(765): Unexpected value from nativeGetEnabledTags: 0
11-30 17:26:35.681: W/Trace(765): Unexpected value from nativeGetEnabledTags: 0
11-30 17:26:35.681: W/Trace(765): Unexpected value from nativeGetEnabledTags: 0
11-30 17:26:35.961: D/dalvikvm(765): GC_CONCURRENT freed 270K, 14% free 2571K/2968K,     paused 18ms+3ms, total 56ms
11-30 17:26:35.961: D/dalvikvm(765): WAIT_FOR_CONCURRENT_GC blocked 38ms
11-30 17:26:36.131: W/Trace(765): Unexpected value from nativeGetEnabledTags: 0
11-30 17:26:36.140: W/Trace(765): Unexpected value from nativeGetEnabledTags: 0
11-30 17:26:36.140: W/Trace(765): Unexpected value from nativeGetEnabledTags: 0
11-30 17:26:36.241: D/dalvikvm(765): GC_CONCURRENT freed 239K, 12% free 2805K/3172K,     paused 21ms+8ms, total 64ms
11-30 17:26:36.261: W/Trace(765): Unexpected value from nativeGetEnabledTags: 0
11-30 17:26:36.301: W/Trace(765): Unexpected value from nativeGetEnabledTags: 0
11-30 17:26:36.331: W/Trace(765): Unexpected value from nativeGetEnabledTags: 0
11-30 17:26:36.341: W/Trace(765): Unexpected value from nativeGetEnabledTags: 0
11-30 17:26:36.371: W/Trace(765): Unexpected value from nativeGetEnabledTags: 0
11-30 17:26:36.371: W/Trace(765): Unexpected value from nativeGetEnabledTags: 0
11-30 17:26:36.391: W/Trace(765): Unexpected value from nativeGetEnabledTags: 0
11-30 17:26:36.391: W/Trace(765): Unexpected value from nativeGetEnabledTags: 0
11-30 17:26:36.400: W/Trace(765): Unexpected value from nativeGetEnabledTags: 0
11-30 17:26:36.400: W/Trace(765): Unexpected value from nativeGetEnabledTags: 0
11-30 17:26:36.421: D/gralloc_goldfish(765): Emulator without GPU emulation detected.
11-30 17:26:36.451: W/Trace(765): Unexpected value from nativeGetEnabledTags: 0
11-30 17:26:36.501: W/Trace(765): Unexpected value from nativeGetEnabledTags: 0
11-30 17:26:39.158: W/Trace(765): Unexpected value from nativeGetEnabledTags: 0
11-30 17:26:39.161: W/Trace(765): Unexpected value from nativeGetEnabledTags: 0
11-30 17:26:39.171: W/Trace(765): Unexpected value from nativeGetEnabledTags: 0
11-30 17:26:39.171: W/Trace(765): Unexpected value from nativeGetEnabledTags: 0
11-30 17:26:39.191: W/Trace(765): Unexpected value from nativeGetEnabledTags: 0
11-30 17:26:39.191: W/Trace(765): Unexpected value from nativeGetEnabledTags: 0
11-30 17:26:39.281: W/Trace(765): Unexpected value from nativeGetEnabledTags: 0
11-30 17:26:39.301: W/Trace(765): Unexpected value from nativeGetEnabledTags: 0
11-30 17:26:39.310: D/AndroidRuntime(765): Shutting down VM
11-30 17:26:39.310: W/dalvikvm(765): threadid=1: thread exiting with uncaught exception     (group=0x40a70930)
11-30 17:26:39.321: E/AndroidRuntime(765): FATAL EXCEPTION: main
11-30 17:26:39.321: E/AndroidRuntime(765): java.lang.IllegalArgumentException: View not     attached to window manager
11-30 17:26:39.321: E/AndroidRuntime(765):  at     android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:383)
11-30 17:26:39.321: E/AndroidRuntime(765):  at     android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:285)
11-30 17:26:39.321: E/AndroidRuntime(765):  at     android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:79)
11-30 17:26:39.321: E/AndroidRuntime(765):  at     android.widget.Toast$TN.handleShow(Toast.java:396)
11-30 17:26:39.321: E/AndroidRuntime(765):  at     android.widget.Toast$TN$1.run(Toast.java:310)
11-30 17:26:39.321: E/AndroidRuntime(765):  at     android.os.Handler.handleCallback(Handler.java:725)
11-30 17:26:39.321: E/AndroidRuntime(765):  at     android.os.Handler.dispatchMessage(Handler.java:92)
11-30 17:26:39.321: E/AndroidRuntime(765):  at android.os.Looper.loop(Looper.java:137)
11-30 17:26:39.321: E/AndroidRuntime(765):  at     android.app.ActivityThread.main(ActivityThread.java:5039)
11-30 17:26:39.321: E/AndroidRuntime(765):  at     java.lang.reflect.Method.invokeNative(Native Method)
11-30 17:26:39.321: E/AndroidRuntime(765):  at     java.lang.reflect.Method.invoke(Method.java:511)
11-30 17:26:39.321: E/AndroidRuntime(765):  at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-30 17:26:39.321: E/AndroidRuntime(765):  at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-30 17:26:39.321: E/AndroidRuntime(765):  at dalvik.system.NativeStart.main(Native     Method)
11-30 17:26:42.031: I/Process(765): Sending signal. PID: 765 SIG: 9
Was it helpful?

Solution

I think the problem lies in the code at this toast.setView(arg1); because the same view can't not be attached to multiple objects. as this view is already attached to Gallery item.

Update You just need to recreate that view by layoutInflator using the same values of the current view and then pass this new view to the Toast.

OTHER TIPS

That's because your arg1 view already has a parent (the gallery)...and a view can only have one single parent.

It means you can't attach it to the Toast unless you remove it from the gallery first

Don't use 'getApplicationContext()' for UI stuff, use the Activity context instead. E.g.


public class MyListener implements AdapterView.OnItemClickListener {

    private final Context mContext;

    public MyListener(Activity ctx) {
      mContext = ctx;
    }

    @Override
    public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
        Toast toast = new Toast(mContext);
        toast.setView(arg1);
        toast.show();

    }}

Even better would be to implement AdapterView.OnItemClickListener in your activity to simplify things:


public class MainActivity implements AdapterView.OnItemClickListener {
//start onCreate
gal.setOnItemClickListener(this);
//end onCreate

@Override
    public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
        Toast toast = new Toast(this);
        toast.setView(arg1);
        toast.show();

    }}

If that doesn't fix it, update your question with a stack trace.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top