Android 1.6 & Fragment & Tabhost

Vivi picture Vivi · Jul 2, 2011 · Viewed 9.9k times · Source

I'm working on upgrading an Android application (1.6 compatibility) which uses a TabHost to show 3 different tabs with nested activities.

At the time I used the ActivityGroup trick to show nested activities in a tab but I'm very unhappy with this method since it's a real pain to handle some features.

I heard about the Fragments API compatibility package for 1.6 and a Fragment looks perfect for what I want to do (show nested views / features within a tab with transition effects and stuff) but I can't make it work with a TabHost (It was meant to work with an Action Bar but it's not available in the compatibility package).

Did any of you guys found a way to create such a structure in your applications?

My error here is :

ERROR/AndroidRuntime(955): Caused by: java.lang.RuntimeException: Unable

to start activity ComponentInfo{com.XXX}: java.lang.IllegalArgumentException: No view found for id 0x1020011 for fragment MyFragment

CODE

main.xml

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:layout_weight="1" />

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0" />

    </LinearLayout>

</TabHost>

MainActivity.java

public class MainActivity extends TabActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        Resources res = getResources();
        final TabHost tabs = getTabHost(); 
        TabHost.TabSpec spec;
        Intent i;

        i = new Intent(this, MyActivity.class);
        spec = tabs.newTabSpec("MyActivity").setIndicator("MyActivity",res.getDrawable(R.drawable.tab)).setContent(i);
        tabs.addTab(spec);

    }

}

MyActivity.class

public class MyActivity extends FragmentActivity {

    private static String TAG = "MyActivity";
    private static FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        ListeResultatFragment fragment = MyFragment.newInstance();
        fragmentTransaction.add(android.R.id.tabcontent, fragment, "MyFragment");
        fragmentTransaction.commit();

    }

}

MyFragment.java

public class MyFragment extends Fragment {

    public static MyFragment newInstance() {
        MyFragment instance = new MyFragment();
        return instance;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment, container, false);
    }

}

Answer

Stefan M&#252;cke picture Stefan Mücke · Jul 7, 2011

The problem is that your MyActivity tries to reach out to the enclosing MainActivity by using android.R.id.tabcontent as a container id. This is not possible. Instead, MyActivity needs to have its own layout (e.g. a FrameLayout) which can be used as the parent for the Fragment. In this layout, there must exist a view that can be referenced by id.

Let's say you have a layout called activity_layout.xml which contains a FrameLayout with the id 'framelayout'. You can then modify the onCreate method in MyActivity to something like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View parent = getLayoutInflater().inflate(R.layout.activity_layout, null);
    setContentView(parent);

    fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    Fragment fragment = MyFragment.newInstance();
    fragmentTransaction.add(R.id.framelayout, fragment, "MyFragment");
    fragmentTransaction.commit();
}

In other words, MyActivity needs to be able to work on its own. Try to make it work first, and then embed MyActivity in the MainActivity containing the TabHost.