我想添加tabhost。选项卡1是“历史记录”选项卡(HistoryActivity.java)。选项卡2是收藏夹选项卡,选项卡3是Word选项卡。但是当我跑步时,会发生错误。我不知道原因。我的班级必须从Fragment延伸。
这是TabViewActivity.java
package com.example.android.navigationdrawerexample;
import android.os.Bundle;
import android.content.Intent;
import android.support.v4.app.FragmentTabHost;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.app.Fragment;
import android.app.LocalActivityManager;
import android.app.TabActivity;
import android.widget.TabHost.OnTabChangeListener;
public class TabViewActivity extends Fragment implements OnTabChangeListener {
/** Called when the activity is first created. */
TabHost tabHost;
private View mRoot;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mRoot = inflater.inflate(R.layout.activity_tab_view, null);
tabHost = (TabHost) mRoot.findViewById(android.R.id.tabhost);
// Get TabHost Refference
tabHost.setup();
// Set TabChangeListener called when tab changed
tabHost.setOnTabChangedListener(this);
TabHost.TabSpec spec;
Intent intent;
/************* TAB1 ************/
// Create Intents to launch an Activity for the tab (to be reused)
intent = new Intent(getActivity(), HistoryActivity.class);
spec = tabHost.newTabSpec("First").setIndicator("").setContent(intent);
// Add intent to tab
tabHost.addTab(spec);
/************* TAB2 ************/
intent = new Intent(getActivity(), FavouriteActivity.class);
spec = tabHost.newTabSpec("Second").setIndicator("").setContent(intent);
tabHost.addTab(spec);
/************* TAB3 ************/
intent = new Intent(getActivity(), WoDActivity.class);
spec = tabHost.newTabSpec("Third").setIndicator("").setContent(intent);
tabHost.addTab(spec);
// Set drawable images to tab
tabHost.getTabWidget().getChildAt(1)
.setBackgroundResource(R.drawable.favourite_off);
tabHost.getTabWidget().getChildAt(2)
.setBackgroundResource(R.drawable.wod_off);
// Set Tab1 as Default tab and change image
tabHost.getTabWidget().setCurrentTab(0);
tabHost.getTabWidget().getChildAt(0)
.setBackgroundResource(R.drawable.history_on);
return mRoot;
}
@Override
public void onTabChanged(String tabId) {
/************ Called when tab changed *************/
// ********* Check current selected tab and change according images
// *******/
for (int i = 0; i <tabHost.getTabWidget().getChildCount(); i++) {
if (i == 0)
tabHost.getTabWidget().getChildAt(i)
.setBackgroundResource(R.drawable.history_off);
else if (i == 1)
tabHost.getTabWidget().getChildAt(i)
.setBackgroundResource(R.drawable.favourite_off);
else if (i == 2)
tabHost.getTabWidget().getChildAt(i)
.setBackgroundResource(R.drawable.wod_off);
}
Log.i("tabs", "CurrentTab: " + tabHost.getCurrentTab());
if (tabHost.getCurrentTab() == 0)
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
.setBackgroundResource(R.drawable.history_on);
else if (tabHost.getCurrentTab() == 1)
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
.setBackgroundResource(R.drawable.favourite_on);
else if (tabHost.getCurrentTab() == 2)
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
.setBackgroundResource(R.drawable.wod_on);
}
}
activity_tab_view.xml
<?XML version="1.0" encoding="utf-8"?>
<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">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
</TabHost>
HistoryActivity.java
package com.example.android.navigationdrawerexample;
import android.os.Bundle;
import android.app.Activity;
public class HistoryActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_history);
}
}
tab_history.xml
<?XML version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Screen Design for Food tab -->
<TextView android:text="FOOD TAB DATA"
android:padding="15dip"
android:textSize="18dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
错误:
02-22 22:21:54.075: E/AndroidRuntime(26289): FATAL EXCEPTION: main
02-22 22:21:54.075: E/AndroidRuntime(26289): java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
02-22 22:21:54.075: E/AndroidRuntime(26289): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:703)
02-22 22:21:54.075: E/AndroidRuntime(26289): at android.widget.TabHost.setCurrentTab(TabHost.java:369)
02-22 22:21:54.075: E/AndroidRuntime(26289): at android.widget.TabHost.addTab(TabHost.java:247)
02-22 22:21:54.075: E/AndroidRuntime(26289): at com.example.android.navigationdrawerexample.TabViewActivity.onCreateView(TabViewActivity.java:42)
02-22 22:21:54.075: E/AndroidRuntime(26289): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
02-22 22:21:54.075: E/AndroidRuntime(26289): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
02-22 22:21:54.075: E/AndroidRuntime(26289): at android.app.BackStackRecord.run(BackStackRecord.java:635)
02-22 22:21:54.075: E/AndroidRuntime(26289): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
02-22 22:21:54.075: E/AndroidRuntime(26289): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
02-22 22:21:54.075: E/AndroidRuntime(26289): at android.os.Handler.handleCallback(Handler.java:615)
02-22 22:21:54.075: E/AndroidRuntime(26289): at android.os.Handler.dispatchMessage(Handler.java:92)
02-22 22:21:54.075: E/AndroidRuntime(26289): at android.os.Looper.loop(Looper.java:137)
02-22 22:21:54.075: E/AndroidRuntime(26289): at android.app.ActivityThread.main(ActivityThread.java:4898)
02-22 22:21:54.075: E/AndroidRuntime(26289): at java.lang.reflect.Method.invokeNative(Native Method)
02-22 22:21:54.075: E/AndroidRuntime(26289): at java.lang.reflect.Method.invoke(Method.java:511)
02-22 22:21:54.075: E/AndroidRuntime(26289): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
02-22 22:21:54.075: E/AndroidRuntime(26289): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
02-22 22:21:54.075: E/AndroidRuntime(26289): at dalvik.system.NativeStart.main(Native Method)