我得到以下内容" java.lang.IllegalArgumentException:视图未附加到窗口管理器"
当调用 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
答案 0 :(得分:4)
我认为问题在于此toast.setView(arg1);
的代码,因为相同的视图不能附加到多个对象。因为此视图已附加到图库项目。
<强>更新强>
您只需要使用当前视图的相同值通过layoutInflator
重新创建该视图,然后将此新视图传递给Toast。
答案 1 :(得分:1)
那是因为您的arg1
视图已经有父级(图库)......并且视图只能有一个父级。
这意味着您无法将其附加到Toast
,除非您先将其从图库中删除
答案 2 :(得分:1)
不要对UI内容使用'getApplicationContext()',而是使用Activity上下文。 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();
}}
更好的方法是在您的活动中实现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();
}}
如果这不能解决问题,请使用堆栈跟踪更新您的问题。