0

我得到以下“ java.lang.IllegalArgumentException: View not attach to window manager

当调用toast.show()时会发生这种情况。

请问有什么解释吗?非常感谢!!!!

编辑:我发布所有代码+ logcat

代码 - 注意在侦听器中调用toast.show()时会发生错误。

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
4

3 回答 3

4

我认为问题出在代码上,toast.setView(arg1); 因为同一个视图不能不附加到多个对象。因为此视图已附加到图库项目。

更新 您只需要layoutInflator使用当前视图的相同值重新创建该视图,然后将这个新视图传递给 Toast。

于 2012-11-30T15:14:49.570 回答
1

那是因为您的arg1视图已经有一个父视图(画廊)......而一个视图只能有一个父视图。

这意味着您无法将其附加到Toast除非您先将其从图库中删除

于 2012-11-30T15:11:22.620 回答
1

不要对 UI 内容使用“getApplicationContext()”,而是使用 Activity 上下文。例如


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();

    }}

更好的是在您的活动中实现 AdapterView.OnItemClickListener 以简化事情:


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();

    }}

如果这不能解决问题,请使用堆栈跟踪更新您的问题。

于 2012-11-30T16:38:58.353 回答