我有一个基于tabHost的应用程序,带有片段, 如果我将片段用于LinearLayout
,它可以正常工作但我需要在其中一个标签视图[片段]
上有一个列表但应用程序崩溃是因为未正确充气: 我的班级[tab view Fragment]:
public class Tab2Fragment extends Fragment { //primera pregunta, lo puedo hacer asi o con FragmentActivity
//public class Tab2Fragment extends ListFragment { //primera pregunta, lo puedo hacer asi o con FragmentActivity
private FragmentManager fm;
private ListFragment list;
private List<Map<String, String>> listItems;
private String[] froms;
private int[] viewIds;
/* (non-Javadoc)
* @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
// We have different layouts, and in one of them this
// fragment's containing frame doesn't exist. The fragment
// may still be created from its saved state, but there is
// no reason to try to create its view hierarchy because it
// won't be displayed. Note this is not needed -- we could
// just run the code below, where we would create and return
// the view hierarchy; it would just never be used.
return null;
}
return (LinearLayout)inflater.inflate(R.layout.tab_frag2_layout, container, false);
}
}
我的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00FF00">
<fragment
android:name="android.support.v4.app.ListFragment"
android:id="@+id/List"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
错误:
05-31 22:39:58.412: E/AndroidRuntime(2796): FATAL EXCEPTION: main
05-31 22:39:58.412: E/AndroidRuntime(2796): android.view.InflateException: Binary XML file line #8: Error inflating class fragment
05-31 22:39:58.412: E/AndroidRuntime(2796): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:582)
05-31 22:39:58.412: E/AndroidRuntime(2796): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
05-31 22:39:58.412: E/AndroidRuntime(2796): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
05-31 22:39:58.412: E/AndroidRuntime(2796): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
05-31 22:39:58.412: E/AndroidRuntime(2796): at com.orchard.elasto.Tab2Fragment.onCreateView(Tab2Fragment.java:54)
05-31 22:39:58.412: E/AndroidRuntime(2796): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:870)
那么,如何正确地扩充名单呢?
我应该使用fragmentList,即使tabHost正在编写片段吗?
public void onTabChanged(String tag) {
TabInfo newTab = this.mapTabInfo.get(tag);
if (mLastTab != newTab) {
FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
if (mLastTab != null) {
if (mLastTab.fragment != null) {
ft.detach(mLastTab.fragment);
}
}
if (newTab != null) {
if (newTab.fragment == null) {
newTab.fragment = Fragment.instantiate(this,
newTab.clss.getName(), newTab.args);
ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag);
} else {
ft.attach(newTab.fragment);
}
}
mLastTab = newTab;
ft.commit();
this.getSupportFragmentManager().executePendingTransactions();
}
}
非常感谢!!
答案 0 :(得分:1)
你的问题在这里:
android:name="android.support.v4.app.ListFragment"
您告诉它运行该片段的类是ListFragment。
您需要更改它以引用您的类,并且您需要使该类成为ListFragment而不是片段。
android:name="your.package.name.Tab2Fragment"