Reputation: 16128
I have a tabHost based app, with fragments, It works fine if I use my fragments with a LinearLayout
but I need to have a list on one of the tabs views [fragment]
but the app crashes because is not correctly inflated: my class [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);
}
}
my 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>
the error:
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)
So, how to properly inflate the list?
should i do it with fragmentList, even when the tabHost is specting a Fragment?
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();
}
}
thanks a lot!!
Upvotes: 0
Views: 1019
Reputation: 16393
Your problem is here:
android:name="android.support.v4.app.ListFragment"
You are telling it that the class that runs that fragment is ListFragment.
You need to change it to reference your class, and you need to make that class a ListFragment instead of a Fragment.
android:name="your.package.name.Tab2Fragment"
Upvotes: 1