问题
I want to have a Tabhost inside a Fragment and got the Exception
did you forget to call public void setup(localactivitymanager activitygroup)
Here my Code:
layout:
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
Fragment:
public class TabsFragment extends SherlockFragment implements OnTabChangeListener{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
this.contentView = inflater.inflate(R.layout.fragment_details, container, false);
mTabHost = (FragmentTabHost) this.contentView.findViewById(android.R.id.tabhost);
return this.contentView;
}
@Override
public void onResume() {
super.onResume();
mTabHost.setup(getSherlockActivity(),getChildFragmentManager());
mTabHost.setOnTabChangedListener(this);
// First Tab
String s = getResources().getString(R.string.title_tab1);
TabSpec spec = mTabHost.newTabSpec(s);
Intent i = new Intent(getSherlockActivity(), FirstTab.class);
spec.setIndicator(s);
spec.setContent(i);
mTabHost.addTab(spec);
// Adding more tabs here
...
}
Like you can see I've already called the setup. I've got no idea why the exception is thrown :(
Hopefully someone could help me.
Thx!
回答1:
public void setup (LocalActivityManager activityGroup)
If you are using setContent(android.content.Intent), this must be called since the activityGroup is needed to launch the local activity. This is done for you if you extend TabActivity.
Android Exception: Did you forget to call 'public void setup (LocalActivityManager activityGroup)'
回答2:
You shoud addTab in so way:
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
FragmentStackSupport.CountingFragment.class, null);
As you can see you have to use Fragment class (not an Activity) and last argument should be bundle (not an intent)
回答3:
you are extending Activity class, try extending TabActivity class
or
if you want to use the Activity class then use the following :
host = (TabHost) findViewById(R.id.tabhost); //here tabHost will be your Tabhost
LocalActivityManager mLocalActivityManager = new LocalActivityManager(mActivity, false);
mLocalActivityManager.dispatchCreate(state); // state will be bundle your activity state which you get in onCreate
tabHost.setup(mLocalActivityManager);
来源:https://stackoverflow.com/questions/23625953/tabhost-in-fragment-did-you-forget-to-call-public-void-setuplocalactivitymana