Android中Fragment内的Tab

14

我正在尝试像示例一样在片段中添加TabHost,但在调用

mTabHost.setup()

时出现NullPointerException,可能是什么问题?以下是代码

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.tabs_layout,container,false);

    mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
    mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.tabcontent);

    mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("tab1"),
            Fragment1.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("tab2"),
            Fragment2.class, null);
    return rootView;
}

XML:

<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:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:id="@+id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />

    </LinearLayout>
</android.support.v4.app.FragmentTabHost>

你得到的异常的堆栈跟踪是什么? - barq
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.tabcontent); 这行代码会导致 nullpointerexception 异常。 - Jaky71
展示你的标签布局。 - barq
它的问题布局几乎相同。 - Jaky71
请确保您已更新android-support-v4.jar文件。 此外,此功能适用于API级别17。 也请检查这一点。 - Shahinoor Shahin
6个回答

2
以下代码是我使用的示例:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    mView = inflater.inflate(R.layout.fragment_tab_destaques, container, false);

    mTabHost = (TabHost) mView.findViewById(android.R.id.tabhost);

    initialiseTabHost();   

    return mView;
}

private void initialiseTabHost() {

    mTabHost.setup();

    String title = "TITLE";

    addTab(getActivity(), this.mTabHost, this.mTabHost.newTabSpec(title).setIndicator(title));

    Typeface tf = Typeface.createFromAsset(getActivity().getAssets(),
            "fonts/Roboto-Bold.ttf");

    for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {

        TextView tv = (TextView) mTabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);

        tv.setTextAppearance(getActivity(), android.R.style.TextAppearance_Medium);
        tv.setTextColor(Color.WHITE);
        tv.setTypeface(tf);

        mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.selector_actionbar);

    }

    mTabHost.setOnTabChangedListener(this);

}

private static void addTab(Activity activity, TabHost tabHost, TabHost.TabSpec tabSpec) {

    tabSpec.setContent(new InicialTabFactory(activity));
    tabHost.addTab(tabSpec);

}

My fragment_tab_destaques.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    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"
    android:orientation="vertical">

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <HorizontalScrollView
                android:id="@+id/horizontalScrollView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fillViewport="true"
                android:scrollbars="none"
                android:background="@color/BlueViolet">

                <TabWidget
                    android:id="@android:id/tabs"
                    android:orientation="horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:backgroundStacked="@color/white"
                    android:weightSum="2" />

            </HorizontalScrollView>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="0" />

        </LinearLayout>

    </TabHost>

</LinearLayout>

EDIT

InicialTabFactory.java:

public class InicialTabFactory implements TabHost.TabContentFactory {

    private final Context mContext;

    public InicialTabFactory(Context context) {

        mContext = context;

    }

    @Override
    public View createTabContent(String s) {

        View v = new View(mContext);
        v.setMinimumWidth(0);
        v.setMinimumHeight(0);

        return v;
    }

}

“addTab”未定义为类型…错误。是否存在名为“addTab”的函数? - Jaky71
Lennon,这次InicialTabFactory未定义。这是一个导入还是函数? - Jaky71
抱歉,我忘记将那个类添加到答案中了。我编辑了我的回答,并且也看了一下 onCreateView,因为我忘记在那里调用 initialiseTabHost(); - Lennon Spirlandelli
Lennon,感谢你的努力。我找到了一个解决方案,已经在上面发布了。+1 为你的努力。 - Jaky71
好的,我很高兴你找到了解决方案。 - Lennon Spirlandelli
显示剩余3条评论

2
在这里解决您的问题+一个漂亮的材料设计,请点击此处

欢迎来到 Stack Overflow!如目标网站无法访问或永久下线,请引用链接中最相关的部分。请参阅 如何撰写好答案 - Maveňツ

2

试试这个...

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    tabHost = new FragmentTabHost(getActivity());
    tabHost.setup(getActivity(), getChildFragmentManager(), R.layout.fragment_home);

    Bundle arg1 = new Bundle();
    arg1.putInt("Arg for Frag1", 1);
    tabHost.addTab(tabHost.newTabSpec("Tab1").setIndicator("tab1"),
            Tab1Fragment.class, arg1);

    Bundle arg2 = new Bundle();
    arg2.putInt("Arg for Frag2", 2);
    tabHost.addTab(tabHost.newTabSpec("Tab2").setIndicator("tab2"),
        Tab2Fragment.class, arg2);

    Bundle arg3 = new Bundle();
    arg3.putInt("Arg for Frag3", 3);
    tabHost.addTab(tabHost.newTabSpec("Tab3").setIndicator("tab3"),
        Tab3Fragment.class, arg3);

    return tabHost;

}

欢迎来到 Stack Overflow!请引用代码中最相关的部分。请参阅 如何撰写好答案 - Maveňツ

2

我的简单工作示例

Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Reusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, ArtistsActivity.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("artists").setIndicator("Artists",
                      res.getDrawable(R.drawable.ic_tab_artists))
                  .setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, AlbumsActivity.class);
    spec = tabHost.newTabSpec("albums").setIndicator("Albums",
                      res.getDrawable(R.drawable.ic_tab_albums))
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, SongsActivity.class);
    spec = tabHost.newTabSpec("songs").setIndicator("Songs",
                      res.getDrawable(R.drawable.ic_tab_songs))
                  .setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(2);
}


<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"
        android:padding="5dp">
        <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"
            android:padding="5dp" />
    </LinearLayout>
</TabHost>

2
我已经在片段中使用了带有ViewPager的选项卡。这是我的代码:-
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_dashboard_tab, container, false);

    mViewPager = (ViewPager) rootView.findViewById(R.id.pager);
    mViewPager.setAdapter(new TabPageAdapter(getChildFragmentManager(), getActivity()));
    tabHost = (TabHost) rootView.findViewById(android.R.id.tabhost);
    tabHost.setup();
    mViewPager.setOnPageChangeListener(this);
    for (int i = 0; i < tabSpec.length; i++) {
        tabHost.addTab(tabHost.newTabSpec(tabSpec[i]).setIndicator(tabTitle[i]).setContent(mFactory));
    }

    tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
        @Override
        public void onTabChanged(String tabId) {

            if (tabId.equals("Tab_1")) {
                mViewPager.setCurrentItem(0);
            } else if (tabId.equals("Tab_2")) {
                mViewPager.setCurrentItem(1);
            } else if (tabId.equals("Tab_3")) {
                mViewPager.setCurrentItem(2);
            } else if (tabId.equals("Tab_4")) {
                mViewPager.setCurrentItem(3);
            }
        }

    });

    return rootView;
}

这是布局文件:-
<?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="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" />

<FrameLayout
    android:id="@android:id/tabcontent"
    android:layout_width="match_parent"
    android:layout_height="0dp" />

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"/>
</LinearLayout>

</TabHost>

希望这能对你有所帮助 :)

1

[已解决] 这是我想要的解决方案。有一个主要的 fragment,里面有 tabs。解决方案在 这里

可能会出现链接失效的情况,这里提供代码:

    package com.example.android.apis.app;

/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.android.apis.app;

import java.util.ArrayList;

import com.example.android.apis.R;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;

/**
 * Sample fragment that contains tabs of other fragments.
 */
public class FragmentTabsFragment extends Fragment {
    TabManager mTabManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTabManager = new TabManager(getActivity(), getChildFragmentManager(),
                R.id.realtabcontent);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_tabs_fragment, container, false);
        TabHost host = mTabManager.handleCreateView(v);

        mTabManager.addTab(host.newTabSpec("result").setIndicator("Result"),
                FragmentReceiveResult.ReceiveResultFragment.class, null);
        mTabManager.addTab(host.newTabSpec("contacts").setIndicator("Contacts"),
                LoaderCursor.CursorLoaderListFragment.class, null);
        mTabManager.addTab(host.newTabSpec("apps").setIndicator("Apps"),
                LoaderCustom.AppListFragment.class, null);
        mTabManager.addTab(host.newTabSpec("throttle").setIndicator("Throttle"),
                LoaderThrottle.ThrottledLoaderListFragment.class, null);

        return v;
    }

    @Override
    public void onViewStateRestored(Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
        mTabManager.handleViewStateRestored(savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mTabManager.handleDestroyView();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mTabManager.handleSaveInstanceState(outState);
    }

    /**
     * This is a helper class that implements a generic mechanism for
     * associating fragments with the tabs in a tab host.  DO NOT USE THIS.
     * If you want tabs in a fragment, use the support v13 library's
     * FragmentTabHost class, which takes care of all of this for you (in
     * a simpler way even).
     */
    public static class TabManager implements TabHost.OnTabChangeListener {
        private final Context mContext;
        private final FragmentManager mManager;
        private final int mContainerId;
        private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
        private TabHost mTabHost;
        private TabInfo mLastTab;
        private boolean mInitialized;
        private String mCurrentTabTag;

        static final class TabInfo {
            private final String tag;
            private final Class<?> clss;
            private final Bundle args;
            private Fragment fragment;

            TabInfo(String _tag, Class<?> _class, Bundle _args) {
                tag = _tag;
                clss = _class;
                args = _args;
            }
        }

        static class DummyTabFactory implements TabHost.TabContentFactory {
            private final Context mContext;

            public DummyTabFactory(Context context) {
                mContext = context;
            }

            @Override
            public View createTabContent(String tag) {
                View v = new View(mContext);
                v.setMinimumWidth(0);
                v.setMinimumHeight(0);
                return v;
            }
        }

        public TabManager(Context context, FragmentManager manager, int containerId) {
            mContext = context;
            mManager = manager;
            mContainerId = containerId;
        }

        public TabHost handleCreateView(View root) {
            if (mTabHost != null) {
                throw new IllegalStateException("TabHost already set");
            }
            mTabHost = (TabHost)root.findViewById(android.R.id.tabhost);
            mTabHost.setup();
            mTabHost.setOnTabChangedListener(this);
            return mTabHost;
        }

        public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
            tabSpec.setContent(new DummyTabFactory(mContext));
            String tag = tabSpec.getTag();
            TabInfo info = new TabInfo(tag, clss, args);
            mTabs.add(info);
            mTabHost.addTab(tabSpec);
        }

        public void handleViewStateRestored(Bundle savedInstanceState) {
            if (savedInstanceState != null) {
                mCurrentTabTag = savedInstanceState.getString("tab");
            }
            mTabHost.setCurrentTabByTag(mCurrentTabTag);

            String currentTab = mTabHost.getCurrentTabTag();

            // Go through all tabs and make sure their fragments match
            // the correct state.
            FragmentTransaction ft = null;
            for (int i=0; i<mTabs.size(); i++) {
                TabInfo tab = mTabs.get(i);
                tab.fragment = mManager.findFragmentByTag(tab.tag);
                if (tab.fragment != null && !tab.fragment.isDetached()) {
                    if (tab.tag.equals(currentTab)) {
                        // The fragment for this tab is already there and
                        // active, and it is what we really want to have
                        // as the current tab.  Nothing to do.
                        mLastTab = tab;
                    } else {
                        // This fragment was restored in the active state,
                        // but is not the current tab.  Deactivate it.
                        if (ft == null) {
                            ft = mManager.beginTransaction();
                        }
                        ft.detach(tab.fragment);
                    }
                }
            }

            // We are now ready to go.  Make sure we are switched to the
            // correct tab.
            mInitialized = true;
            ft = doTabChanged(currentTab, ft);
            if (ft != null) {
                ft.commit();
                mManager.executePendingTransactions();
            }
        }

        public void handleDestroyView() {
            mCurrentTabTag = mTabHost.getCurrentTabTag();
            mTabHost = null;
            mTabs.clear();
            mInitialized = false;
        }

        public void handleSaveInstanceState(Bundle outState) {
            outState.putString("tab", mTabHost != null
                    ? mTabHost.getCurrentTabTag() : mCurrentTabTag);
        }

        @Override
        public void onTabChanged(String tabId) {
            if (!mInitialized) {
                return;
            }
            FragmentTransaction ft = doTabChanged(tabId, null);
            if (ft != null) {
                ft.commit();
            }
        }

        private FragmentTransaction doTabChanged(String tabId, FragmentTransaction ft) {
            TabInfo newTab = null;
            for (int i=0; i<mTabs.size(); i++) {
                TabInfo tab = mTabs.get(i);
                if (tab.tag.equals(tabId)) {
                    newTab = tab;
                }
            }
            if (newTab == null) {
                throw new IllegalStateException("No tab known for tag " + tabId);
            }
            if (mLastTab != newTab) {
                if (ft == null) {
                    ft = mManager.beginTransaction();
                }
                if (mLastTab != null) {
                    if (mLastTab.fragment != null) {
                        ft.detach(mLastTab.fragment);
                    }
                }
                if (newTab != null) {
                    if (newTab.fragment == null) {
                        newTab.fragment = Fragment.instantiate(mContext,
                                newTab.clss.getName(), newTab.args);
                        ft.add(mContainerId, newTab.fragment, newTab.tag);
                    } else {
                        ft.attach(newTab.fragment);
                    }
                }

                mLastTab = newTab;
            }
            return ft;
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接