向一个片段添加标签

时间:2015-02-23 12:03:31

标签: android android-fragments tabs

我正在尝试将标签添加到我的应用中的一个片段(该应用有几个片段)。我试图让这个开始工作。我发现了一些来自stackoverflow答案的建议,该答案已发布到某人的问题但这会导致我的应用程序崩溃。

片段代码    公共类salesPartsHolderFragment扩展Fragment {

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$1.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)

3 个答案:

答案 0 :(得分:1)

替换

TabHost tabs = (TabHost)getActivity().findViewById(R.id.tabhost);

TabHost tabs = (TabHost)theView.findViewById(R.id.tabhost);

试试这个。这将有效。

调用getActivity.findViewById()无效onCreateView(),因为View的{​​{1}}尚未由Fragment创建。

答案 1 :(得分:1)

您必须制作一个Tab活动,在该活动中您将添加您的碎片。我希望以下代码可以帮助您:

public class TabActivity扩展了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();
}

}

答案 2 :(得分:0)

寻求有关此项目的帮助SlidingTabsBasic。 Google建议使用它而不是TabHost。