本文介绍了ViewStub引发错误而膨胀多个版面条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用程序,我有一个微调,和 ViewStub ,这取决于从飞旋用户选择的项目,我有夸大不同的布局和显示的微调下膨胀的布局。当我的应用程序启动时, ViewStub 成功地膨胀从微调任何项目的第一次选择的布局。当我试图选择微调一个新的项目,它提出了以下
异常 java.lang.IllegalStateException:ViewStub必须有一个非空的ViewGroup viewParent
我的code迄今
@覆盖
公共无效onItemSelected(适配器视图<> pParent,查看PVIEW,INT p位置,长PID){
如果(p位置== 1){
m_cStub.setLayoutResource(R.layout.text_form);
}否则如果(p位置== 2){
m_cStub.setLayoutResource(R.layout.integer_form);
}
查看充气= m_cStub.inflate();
}
m_cStub是 ViewStub内创建对象的onCreate()在活动中
下面是我的主要布局的xml code
< RelativeLayout的..................>
<微调....................... />
< ViewStub机器人:ID =@ + ID / dynamic_form_layout
机器人:inflatedId =@ + ID / dynamic_form_inflated_id
机器人:layout_alignParentBottom =真
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT/>
< / RelativeLayout的>
谁能告诉我在哪里,我错了。如果您有任何其他的解决方案来解决这个问题,请大家分享。
感谢。
解决方案 ViewStub
的目的不是要在像这样的情况下使用
。之后存根膨胀,存根是的
删除的从视图层次结构。这就是为什么它没有父,并提到
IllegalStateException异常
提高。
ViewStub
不能多次使用。同时保持长寿命参照
ViewStub
是不必要的,如果需要,这是很好的做法,
空
之后充气,所以GC可以吃。
考虑使用 addView()
/ removeView()
来代替你的看法。或者更好地利用 ViewFlipper 。
In my app, I am having a spinner, and a ViewStub, depending upon the user item selection from spinner, I have to inflate different layouts and show the inflated layout below the spinner. When my app starts, ViewStub successfully inflates a layout on first time selection of any item from spinner. When I tries to select a new item from spinner, it raises Exception below
java.lang.IllegalStateException: ViewStub must have a non-null ViewGroup viewParent
My code so far is
@Override
public void onItemSelected(AdapterView<?> pParent, View pView, int pPosition, long pId) {
if(pPosition == 1){
m_cStub.setLayoutResource(R.layout.text_form);
}else if(pPosition == 2){
m_cStub.setLayoutResource(R.layout.integer_form);
}
View inflated = m_cStub.inflate();
}
m_cStub is a ViewStub object created inside onCreate() of the Activity.
Here is my main layout xml code
<RelativeLayout..................>
<spinner......................./>
<ViewStub android:id="@+id/dynamic_form_layout"
android:inflatedId="@+id/dynamic_form_inflated_id"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
Can anybody please tell me where I am going wrong. If you have any other solution to solve this please share.
Thanks.
解决方案
ViewStub
is not designed to be used in scenarios like this one. After the stub is inflated, the stub is removed from the view hierarchy. That's why it has no parent and mentioned IllegalStateException
raised. ViewStub
can’t be used more than once. Also keeping long-lived reference to a ViewStub
is unnecessary, if it is required, it's good practice to null
it after inflating, so GC can eat it.
Consider using addView()
/ removeView()
to replace your views. Or better use ViewFlipper.
这篇关于ViewStub引发错误而膨胀多个版面条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-26 22:47