Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 1.6 & Fragment & Tabhost

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);
    }

}
like image 328
Vivi Avatar asked Jul 02 '11 11:07

Vivi


People also ask

What is Android 1.5 called?

Android Cupcake (version 1.5) is the third version of Android developed by Google, a major platform release deployable to Android-powered handsets starting in April 2009, that is no longer supported. The release included new features for users and developers, as well as changes in the Android framework API.

What is Android 12 called?

Android 12 (internally codenamed Snow Cone) was announced in an Android blog posted on February 18, 2021. A developer preview was released immediately, with two additional ones planned the following two months.

What is Android 13 called?

Android 13's internal codename While Google doesn't use these codenames for marketing purposes anymore, its developers still use them internally. For Android 13, this name is Tiramisu, following up on the not-publicly used Snow Cone for Android 12, Red Velvet Cake for Android 11, and Android 10's Quince Tart.


2 Answers

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.

like image 138
Stefan Mücke Avatar answered Oct 15 '22 11:10

Stefan Mücke


Why don't you create your own tabbar. It's verry easy to build. Just add a LinearLayout with some buttons in it and set the onClickListener to switch fragments by using the FragmentManager. The Fragment manager can be obtained from a FragmentActivity.

FragmentManager fragmentManager = getSupportFragmentManager();

In the onClick handler you just do a transaction to the switch to the correct fragments.

like image 24
Kevin Avatar answered Oct 15 '22 12:10

Kevin