Cha*_*har 7 android inflate viewstub layout-inflater
在我的应用程序中,我有一个微调器和一个ViewStub,取决于微调器中的用户项目选择,我必须膨胀不同的布局并在微调器下方显示膨胀的布局.当我的应用程序启动时,ViewStub会在第一次从微调器中选择任何项目时成功膨胀布局.当我尝试从微调器中选择一个新项时,它会在下面引发异常
java.lang.IllegalStateException: ViewStub must have a non-null ViewGroup viewParent
Run Code Online (Sandbox Code Playgroud)
到目前为止我的代码是
@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();
}
Run Code Online (Sandbox Code Playgroud)
m_cStub是在Activity的onCreate()内创建的ViewStub对象.
这是我的主要布局xml代码
<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>
Run Code Online (Sandbox Code Playgroud)
谁能告诉我哪里出错了.如果您有任何其他解决方案可以解决此问题,请分享.
谢谢.
bie*_*eux 23
ViewStub
不适用于像这样的场景.在存根充气后,将从视图层次结构中删除存根.这就是为什么它没有父母,并提到IllegalStateException
提出.ViewStub
不能多次使用.ViewStub
如果需要的话,保持长寿命参考是不必要的,null
在充气后这是很好的做法,所以GC可以吃它.
考虑使用addView()
/ removeView()
替换您的视图.或者更好地使用ViewFlipper.
归档时间: |
|
查看次数: |
7157 次 |
最近记录: |