我正在尝试使用FragmentTabHost
实现嵌套片段,但是我一直在使用setup()
函数出错。代码如下。
这是我的班级档案
public class ForumFragment extends Fragment{
private FragmentTabHost mTabHost;
public ForumFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_forum,container,false);
mTabHost = (FragmentTabHost) view.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(),android.R.id.tabcontent);
mTabHost.addTab(mTabHost.newTabSpec("Tab1").setIndicator("DBoard"), DiscussionBoardFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("Tab2").setIndicator("Events"), EventsFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("Tab3").setIndicator("HW Help"), HWHelpFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("Tab4").setIndicator("Classifieds"), ClassifiedsFragment.class, null);
return view;
}
这是我的XML文件
<TabHost
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:id="@+id/tabhost">
<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">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/DBoard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></RelativeLayout>
<RelativeLayout
android:id="@+id/Events"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></RelativeLayout>
<RelativeLayout
android:id="@+id/HWHelp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></RelativeLayout>
<RelativeLayout
android:id="@+id/Classifieds"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></RelativeLayout>
</FrameLayout>
</LinearLayout>
这是我收到的错误
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.FragmentTabHost.setup(android.content.Context, android.support.v4.app.FragmentManager, int)' on a null object reference
at com.ForumFragment.onCreateView(ForumFragment.java:30)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6897)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
答案 0 :(得分:0)
更改
mTabHost.setup(getActivity(), getChildFragmentManager(),android.R.id.tabcontent);
到
mTabHost.setup(getActivity(), getSupportFragmentManager(),android.R.id.tabcontent);
答案 1 :(得分:0)
问题出在这一行:
mTabHost = (FragmentTabHost) view.findViewById(android.R.id.tabhost);
您需要为自己的R
文件充气,而不是android.R
。只需将其更改为:
mTabHost = (FragmentTabHost) view.findViewById(R.id.tabhost);
并在XML文件中将<TabHost
更改为<android.support.v4.app.FragmentTabHost
答案 2 :(得分:0)
你必须将你的片段转换为 android.support.v4.app.Fragment ,因为你使用的是 android.support.v4.app.FragmentTabHost ,它是< strong>仅支持android.support.v4.app.Fragment。
读取此行 android.support.v4.app.FragmentTabHost.setup(android.content.Context,android.support.v4.app.FragmentManager,int)
答案 3 :(得分:0)
Android Studio不提供依赖项,因此您必须手动导入它们。文件&gt;项目结构&gt; app(在“模块”下)&gt;依赖选项卡。单击底部的“+”以添加库依赖项;在这种情况下,“support-v4”。