ViewStub'parent的ViewGroup [英] ViewStub'parent viewgroup
问题描述
什么是例外的意思
ava.lang.IllegalStateException:ViewStub必须有一个非空的ViewGroup viewParent
我创造查看存根的数组,并将它们添加到的LinearLayout但这异常显示在运行时
的for(int i = 0; I和LT; 1;我++)
{
尝试
{
存根[0] =新ViewStub(getApplicationContext(),R.layout.view_stub_layout); //Viewv=stub[i].inflate(getApplicationContext(),R.layout.view_stub_layout,mainLayout);
//stub[0].setLayoutResource(R.layout.view_stub_layout);
视图V;
V =存根[0] .inflate();
mainLayout.addView(五);
V =零;
}
赶上(例外五){
e.getMessage();
}
}
您 ViewStub
没有父母,这就是为什么你赶上例外。您必须在布局
先加 ViewStub
,之后就可以将其扩充到另一个查看
。
ViewStub
?你真的需要它吗?也许这可以很好的解决方案:
{尝试
View.inflate(getApplicationContext(),R.layout.view_stub_layout,mainLayout);
}赶上(例外五){
e.getMessage();
}
如果你需要不断补充意见:
{尝试
意见[I] = View.inflate(getApplicationContext(),R.layout.view_stub_layout,NULL);
mainLayout.add(意见[I]);
}赶上(例外五){
e.getMessage();
}
What is the meaning of that exception
ava.lang.IllegalStateException: ViewStub must have a non-null ViewGroup viewParent
i am creating an array of View Stub and adding them to a linearLayout but this exception is showing at run time
for(int i=0;i<1;i++)
{
try
{
stub[0]=new ViewStub(getApplicationContext(),R.layout.view_stub_layout);
//Viewv=stub[i].inflate(getApplicationContext(),R.layout.view_stub_layout,mainLayout);
//stub[0].setLayoutResource(R.layout.view_stub_layout);
View v;
v=stub[0].inflate();
mainLayout.addView(v);
v=null;
}
catch(Exception e){
e.getMessage();
}
}
Your ViewStub
don't have a parent, that's why you catch Exception. You must add ViewStub
in Layout
at first, after you can inflate it to another View
.
Why you use ViewStub
? Do you really need it? Maybe it can be good solution:
try {
View.inflate(getApplicationContext(), R.layout.view_stub_layout, mainLayout);
} catch(Exception e){
e.getMessage();
}
If you need to keep added Views:
try {
views[i] = View.inflate(getApplicationContext(), R.layout.view_stub_layout, null);
mainLayout.add(views[i]);
} catch(Exception e){
e.getMessage();
}
这篇关于ViewStub'parent的ViewGroup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!