Android 1.6 & Fragment & Tabhost
Solution 1:
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:
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Viewparent= getLayoutInflater().inflate(R.layout.activity_layout, null);
setContentView(parent);
fragmentManager = getSupportFragmentManager();
FragmentTransactionfragmentTransaction= fragmentManager.beginTransaction();
Fragmentfragment= 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.
Solution 2:
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.
FragmentManagerfragmentManager= getSupportFragmentManager();
In the onClick handler you just do a transaction to the switch to the correct fragments.
Post a Comment for "Android 1.6 & Fragment & Tabhost"