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);
}
}
<fragment>
elements as children of theFrameLayout
in aTabHost
? I would have expected this to work, but I have not tried it personally yet. – SkutchanActionBar
is not available in the compatibly package but you can use github.com/JakeWharton/ActionBarSherlock – Darrel