二进制 XML 文件第 9 行:膨胀类片段时出错
在运行调用具有片段的 Activity 时出现错误膨胀片段错误。
CheckList 是包含 listview 和描述片段的 Activity。
其 xml 代码是:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">
<fragment class="com.uday.honeytest.CheckList$listview"
android:id="@+id/titles" android:layout_weight="1"
android:layout_width="0px" android:layout_height="match_parent" />
<fragment class="com.uday.honeytest.CheckList$Description"
android:id="@+id/details" android:layout_weight="1"
android:layout_width="0px" android:layout_height="match_parent"
android:background="?android:attr/detailsElementBackground" />
</LinearLayout>
CheckList 类是:
public class CheckList extends Activity {
static int position=0;
static CheckList check;
public void onCreate(Bundle saved){
super.onCreate(saved);
setContentView(R.layout.checklist);
}
public static class listview extends ListFragment {
View mConverView;
String[] items;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
xxxxxx..
return mConverView;
}
}
类似我在下面有描述片段。
但在 setContentView 行出现
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.uday.honeytest/com.uday.honeytest.CheckList}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
错误。
我的描述片段类是:编辑:
public static class Description extends Fragment {
View mConverView;
String details[];
public static Description getInstance() {
Description desc=new Description();
return desc;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView textview=new TextView(getActivity());
textview.setLayoutParams(new LayoutParams(MarginLayoutParams.WRAP_CONTENT, MarginLayoutParams.WRAP_CONTENT));
return textview;
}
}
这里做错了什么?
谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的活动 CheckList 应扩展 FragmentActivity 。
Your activity CheckList should extend FragmentActivity .
您确定正确输入了完全限定的类名吗?
您是否尝试过清理项目/修复项目属性?
我在该类中没有看到任何名为
Description
的内容,这可能是问题所在。希望这有帮助。
<fragment class="com.uday.honeytest.CheckList$Description"
is where it's getting the error.Are you sure you have the fully qualified class name typed correctly?
Have you tried cleaning your project/fixing project properties?
I don't see anything named
Description
in that class, that may be the issue.Hope this helps.
我刚刚删除了
listview 片段
中包含膨胀 xml 的onCreateView
代码,并移至onActivityCreated
方法并使其正常工作。ListFragment 具有由单个列表视图组成的默认布局。但是,如果您愿意,您可以通过从 onCreateView(LayoutInflater, ViewGroup, Bundle) 返回您自己的视图层次结构来自定义片段布局。为此,您的视图层次结构必须包含一个 id 为“@android:id/list”的 ListView 对象(如果在代码中则为 list),
因此我的 listview 扩展了 ListFragment 并且不包含 xml 中的 list ,我们可以直接在 onActivityCreated 中 setListAdapter而不是在 onCreateView 中。
I just removed
onCreateView
codewhich contains inflating xml
inlistview fragment
and move intoonActivityCreated
method with that its working.ListFragment has a default layout that consists of a single list view. However, if you desire, you can customize the fragment layout by returning your own view hierarchy from onCreateView(LayoutInflater, ViewGroup, Bundle). To do this, your view hierarchy must contain a ListView object with the id "@android:id/list" (or list if it's in code)
So my listview extends ListFragment and not conatining list in xml , and directly we can setListAdapter in onActivityCreated rather than in onCreateView.