向一个片段添加标签
Add tabs to one fragment
我正在尝试将选项卡添加到我的应用程序中的一个片段(该应用程序有多个片段)。我正在兜圈子试图让它发挥作用。我从发布到某人问题的 Whosebug 答案中找到了一些建议,但这会使我的应用程序崩溃。
片段代码
public class salesPartsHolderFragment 扩展片段 {
public salesPartsHolderFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View theView = inflater.inflate(R.layout.fragment_sales_parts_holder, container, false);
TabHost tabs=(TabHost)getActivity().findViewById(R.id.tabhost);
tabs.setup();
TabHost.TabSpec spec=tabs.newTabSpec("tag1");
spec.setContent(R.id.tab1);//here you define which tab you want to setup
spec.setIndicator("So Close");//here you choose the text showed in the tab
tabs.addTab(spec);
spec=tabs.newTabSpec("tag2");
spec.setContent(R.id.tab2);
spec.setIndicator("Contacts");
tabs.addTab(spec);
return theView;
}
}
XML代码
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.alcontrol.atmis.anticimex.activities.salesPartsHolderFragment">
<TabHost
android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/setting" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="46dp"
/>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/tab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@color/white"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
</FrameLayout>
</LinearLayout>
LogCat
02-23 12:52:16.601 4991-4991/com.alcontrol.atmis.anticimex D/AndroidRuntime﹕ Shutting down VM
02-23 12:52:16.601 4991-4991/com.alcontrol.atmis.anticimex W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa625f288)
02-23 12:52:16.613 4991-4991/com.alcontrol.atmis.anticimex E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.alcontrol.atmis.anticimex.activities.MainActivity.loadSalesPartsFragment(MainActivity.java:244)
at com.alcontrol.atmis.anticimex.activities.SampleFragment.showSalesPartsFragment(SampleFragment.java:292)
at com.alcontrol.atmis.anticimex.activities.SampleFragment.saveDetailsMoveToSalesParts(SampleFragment.java:281)
at com.alcontrol.atmis.anticimex.activities.SampleFragment.onClick(SampleFragment.java:71)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
替换
TabHost tabs = (TabHost)getActivity().findViewById(R.id.tabhost);
和
TabHost tabs = (TabHost)theView.findViewById(R.id.tabhost);
试试这个。这会起作用。
调用 getActivity.findViewById()
在 onCreateView()
中不起作用,因为 Fragment
的 View
尚未由 onCreateView()
创建。
寻求有关此项目的帮助 SlidingTabsBasic。推荐 Google 而不是 TabHost.
您必须制作一个 Tab Activity,在其中 activity 您将添加您的片段。希望以下代码对您有所帮助:
public class TabActivity extends FragmentActivity {
private static TabHost mTabHost;
private TabSpec mTabSpec;
private final String TAG_First = "First";
private final String TAG_Second = "Second";
private final String TAG_Third = "Third";
private int mCurrentTab = 0;
private View mDecorView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
mTabHost = (TabHost) findViewById(R.id.tabhost_lesson);
mTabHost.setOnTabChangedListener(mTabChangeListener);
mTabHost.setup();
setupTabs();
}
private void setupTabs() {
/*
* Setup new tab
*/
mTabSpec = mTabHost.newTabSpec(TAG_First);
mTabHost.setCurrentTab(mCurrentTab);
mTabSpec.setContent(new TabContentFactory() {
@Override
public View createTabContent(String tag) {
return findViewById(android.R.id.tabcontent);
}
});
mTabSpec.setIndicator(createTabContent(TAG_First, 14));
mTabHost.addTab(mTabSpec);
/*
* Setup New Tab
*/
mTabSpec = mTabHost.newTabSpec(TAG_Second);
mTabSpec.setContent(new TabContentFactory() {
@Override
public View createTabContent(String tag) {
return findViewById(android.R.id.tabcontent);
}
});
mTabSpec.setIndicator(createTabContent(TAG_Second, 14));
mTabHost.addTab(mTabSpec);
mTabHost.getTabWidget().setDividerDrawable(null);
/*
* Setup New Tab
*/
mTabSpec = mTabHost.newTabSpec(TAG_Third);
mTabSpec.setContent(new TabContentFactory() {
@Override
public View createTabContent(String tag) {
return findViewById(android.R.id.tabcontent);
}
});
mTabSpec.setIndicator(createTabContent(TAG_Third, 14));
mTabHost.addTab(mTabSpec);
}
/**
* Method to Customize Tabs
*
* @param String
* TabID
* @param Drawable
* drawable
* @param int TabTextSize
* @return
*/
public View createTabContent(String mTabLbl, int mTxtSize) {
View mTabView = LayoutInflater.from(TabActivity.this).inflate(
R.layout.tabs_background, null);
TextView mTabText = (TextView) mTabView.findViewById(R.id.tabsText);
// mTabText.setBackgroundDrawable(mTabDraw);
mTabText.setText(mTabLbl);
return mTabView;
}
public static void SetCurrentTab(int CurrentTab) {
mTabHost.setCurrentTab(CurrentTab);
}
TabHost.OnTabChangeListener mTabChangeListener = new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
if (tabId.equals(TAG_First)) {
mCurrentTab = 0;
FirstFrag fFragment = new FirstFrag();
addTabFragments(tabId, fFragment);
} else if (tabId.equals(TAG_Second)) {
mCurrentTab = 1;
SecondFrag sFragment = new SecondFrag();
addTabFragments(tabId, sFragment);
} else if (tabId.equals(TAG_Third)) {
mCurrentTab = 2;
ThirdFrag tFragment = new ThirdFrag();
addTabFragments(tabId, tFragment);
}
}
};
/**
* Method to add Fragment When Switching Between Tabs
*
* @param String
* Tag
* @param Fragment
* TabFragment
*/
public void addTabFragments(String tag, Fragment mTabFrag) {
FragmentManager mFragmentManager = getSupportFragmentManager();
FragmentTransaction mFragmentTransaction = mFragmentManager
.beginTransaction();
mFragmentTransaction.replace(R.id.frame, mTabFrag);
mFragmentTransaction.commit();
}
}
我正在尝试将选项卡添加到我的应用程序中的一个片段(该应用程序有多个片段)。我正在兜圈子试图让它发挥作用。我从发布到某人问题的 Whosebug 答案中找到了一些建议,但这会使我的应用程序崩溃。
片段代码 public class salesPartsHolderFragment 扩展片段 {
public salesPartsHolderFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View theView = inflater.inflate(R.layout.fragment_sales_parts_holder, container, false);
TabHost tabs=(TabHost)getActivity().findViewById(R.id.tabhost);
tabs.setup();
TabHost.TabSpec spec=tabs.newTabSpec("tag1");
spec.setContent(R.id.tab1);//here you define which tab you want to setup
spec.setIndicator("So Close");//here you choose the text showed in the tab
tabs.addTab(spec);
spec=tabs.newTabSpec("tag2");
spec.setContent(R.id.tab2);
spec.setIndicator("Contacts");
tabs.addTab(spec);
return theView;
}
}
XML代码
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.alcontrol.atmis.anticimex.activities.salesPartsHolderFragment">
<TabHost
android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/setting" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="46dp"
/>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/tab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@color/white"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
</FrameLayout>
</LinearLayout>
LogCat
02-23 12:52:16.601 4991-4991/com.alcontrol.atmis.anticimex D/AndroidRuntime﹕ Shutting down VM
02-23 12:52:16.601 4991-4991/com.alcontrol.atmis.anticimex W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa625f288)
02-23 12:52:16.613 4991-4991/com.alcontrol.atmis.anticimex E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.alcontrol.atmis.anticimex.activities.MainActivity.loadSalesPartsFragment(MainActivity.java:244)
at com.alcontrol.atmis.anticimex.activities.SampleFragment.showSalesPartsFragment(SampleFragment.java:292)
at com.alcontrol.atmis.anticimex.activities.SampleFragment.saveDetailsMoveToSalesParts(SampleFragment.java:281)
at com.alcontrol.atmis.anticimex.activities.SampleFragment.onClick(SampleFragment.java:71)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
替换
TabHost tabs = (TabHost)getActivity().findViewById(R.id.tabhost);
和
TabHost tabs = (TabHost)theView.findViewById(R.id.tabhost);
试试这个。这会起作用。
调用 getActivity.findViewById()
在 onCreateView()
中不起作用,因为 Fragment
的 View
尚未由 onCreateView()
创建。
寻求有关此项目的帮助 SlidingTabsBasic。推荐 Google 而不是 TabHost.
您必须制作一个 Tab Activity,在其中 activity 您将添加您的片段。希望以下代码对您有所帮助:
public class TabActivity extends FragmentActivity {
private static TabHost mTabHost;
private TabSpec mTabSpec;
private final String TAG_First = "First";
private final String TAG_Second = "Second";
private final String TAG_Third = "Third";
private int mCurrentTab = 0;
private View mDecorView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
mTabHost = (TabHost) findViewById(R.id.tabhost_lesson);
mTabHost.setOnTabChangedListener(mTabChangeListener);
mTabHost.setup();
setupTabs();
}
private void setupTabs() {
/*
* Setup new tab
*/
mTabSpec = mTabHost.newTabSpec(TAG_First);
mTabHost.setCurrentTab(mCurrentTab);
mTabSpec.setContent(new TabContentFactory() {
@Override
public View createTabContent(String tag) {
return findViewById(android.R.id.tabcontent);
}
});
mTabSpec.setIndicator(createTabContent(TAG_First, 14));
mTabHost.addTab(mTabSpec);
/*
* Setup New Tab
*/
mTabSpec = mTabHost.newTabSpec(TAG_Second);
mTabSpec.setContent(new TabContentFactory() {
@Override
public View createTabContent(String tag) {
return findViewById(android.R.id.tabcontent);
}
});
mTabSpec.setIndicator(createTabContent(TAG_Second, 14));
mTabHost.addTab(mTabSpec);
mTabHost.getTabWidget().setDividerDrawable(null);
/*
* Setup New Tab
*/
mTabSpec = mTabHost.newTabSpec(TAG_Third);
mTabSpec.setContent(new TabContentFactory() {
@Override
public View createTabContent(String tag) {
return findViewById(android.R.id.tabcontent);
}
});
mTabSpec.setIndicator(createTabContent(TAG_Third, 14));
mTabHost.addTab(mTabSpec);
}
/**
* Method to Customize Tabs
*
* @param String
* TabID
* @param Drawable
* drawable
* @param int TabTextSize
* @return
*/
public View createTabContent(String mTabLbl, int mTxtSize) {
View mTabView = LayoutInflater.from(TabActivity.this).inflate(
R.layout.tabs_background, null);
TextView mTabText = (TextView) mTabView.findViewById(R.id.tabsText);
// mTabText.setBackgroundDrawable(mTabDraw);
mTabText.setText(mTabLbl);
return mTabView;
}
public static void SetCurrentTab(int CurrentTab) {
mTabHost.setCurrentTab(CurrentTab);
}
TabHost.OnTabChangeListener mTabChangeListener = new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
if (tabId.equals(TAG_First)) {
mCurrentTab = 0;
FirstFrag fFragment = new FirstFrag();
addTabFragments(tabId, fFragment);
} else if (tabId.equals(TAG_Second)) {
mCurrentTab = 1;
SecondFrag sFragment = new SecondFrag();
addTabFragments(tabId, sFragment);
} else if (tabId.equals(TAG_Third)) {
mCurrentTab = 2;
ThirdFrag tFragment = new ThirdFrag();
addTabFragments(tabId, tFragment);
}
}
};
/**
* Method to add Fragment When Switching Between Tabs
*
* @param String
* Tag
* @param Fragment
* TabFragment
*/
public void addTabFragments(String tag, Fragment mTabFrag) {
FragmentManager mFragmentManager = getSupportFragmentManager();
FragmentTransaction mFragmentTransaction = mFragmentManager
.beginTransaction();
mFragmentTransaction.replace(R.id.frame, mTabFrag);
mFragmentTransaction.commit();
}
}