如何在Fragment中创建FragmentTabHost?

时间:2014-05-21 07:28:52

标签: android tabs fragment fragment-tab-host

我正在尝试在Android中编写代码,以在Fragment中创建fragmenttabhost。我不知道我做错了什么,但我在addTab中得到了NullPointerException。

我的代码:

MainActivity.java

package com.example.mytab;

import android.app.Fragment;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends TabActivity 
{

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

            Fragment fragment3 = new MyTab();

            Bundle args3 = new Bundle();

            args3.putInt("num", "");

            fragment3.setArguments(args3);

            getFragmentManager().beginTransaction().replace(R.id.content_frame, fragment3).commit();


}   

public static class MyTab extends Fragment
{

        private FragmentTabHost mTabHost;

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

                View rootView;  

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

                 mTabHost = (FragmentTabHost) rootView.findViewById(R.id.tabhost);

                mTabHost.addTab(mTabHost.newTabSpec ("Tab-1") 
                                                    . setIndicator("Tab-1") 
                                                    . setContent(new Intent (getActivity(), PhotosActivity.class)));

                mTabHost.addTab(mTabHost.newTabSpec ("Tab-2") 
                                                    . setIndicator("Tab-2") 
                                                    . setContent(new Intent (getActivity(), SongsActivity.class)));

                    return rootView;


            }

    }
}

XML代码:

mytab.xml

<android.support.v4.app.FragmentTabHost        xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:background="#ffffff"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
    android:orientation="vertical"
    android:background="#ffffff"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TabWidget
        android:id="@android:id/tabs"
        android:background="#000000"
        android:layout_marginLeft="360dp"
        android:showDividers="none"
        android:layout_width="200dp"
        android:layout_height="50dp" />
    <FrameLayout
        android:id="@+id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
</LinearLayout>
  </android.support.v4.app.FragmentTabHost>

错误:

05-21 03:05:05.586: E/AndroidRuntime(2382): FATAL EXCEPTION: main
05-21 03:05:05.586: E/AndroidRuntime(2382): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.keffellowregister/com.example.mytab.MainActivity}: java.lang.NullPointerException
05-21 03:05:05.586: E/AndroidRuntime(2382):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
05-21 03:05:05.586: E/AndroidRuntime(2382):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
05-21 03:05:05.586: E/AndroidRuntime(2382):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-21 03:05:05.586: E/AndroidRuntime(2382):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
05-21 03:05:05.586: E/AndroidRuntime(2382):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 03:05:05.586: E/AndroidRuntime(2382):     at android.os.Looper.loop(Looper.java:137)
05-21 03:05:05.586: E/AndroidRuntime(2382):     at android.app.ActivityThread.main(ActivityThread.java:5103)
05-21 03:05:05.586: E/AndroidRuntime(2382):     at java.lang.reflect.Method.invokeNative(Native Method)
05-21 03:05:05.586: E/AndroidRuntime(2382):     at java.lang.reflect.Method.invoke(Method.java:525)
05-21 03:05:05.586: E/AndroidRuntime(2382):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-21 03:05:05.586: E/AndroidRuntime(2382):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-21 03:05:05.586: E/AndroidRuntime(2382):     at dalvik.system.NativeStart.main(Native Method)
05-21 03:05:05.586: E/AndroidRuntime(2382): Caused by: java.lang.NullPointerException
05-21 03:05:05.586: E/AndroidRuntime(2382):     at android.widget.TabHost.addTab(TabHost.java:236)
05-21 03:05:05.586: E/AndroidRuntime(2382):     at com.example.mytab.MainActivity$MyTab.onCreateView(MainActivity.java:512)
05-21 03:05:05.586: E/AndroidRuntime(2382):     at android.app.Fragment.performCreateView(Fragment.java:1695)
05-21 03:05:05.586: E/AndroidRuntime(2382):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
05-21 03:05:05.586: E/AndroidRuntime(2382):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
05-21 03:05:05.586: E/AndroidRuntime(2382):     at android.app.BackStackRecord.run(BackStackRecord.java:682)
05-21 03:05:05.586: E/AndroidRuntime(2382):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
05-21 03:05:05.586: E/AndroidRuntime(2382):     at android.app.Activity.performStart(Activity.java:5142)
05-21 03:05:05.586: E/AndroidRuntime(2382):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
05-21 03:05:05.586: E/AndroidRuntime(2382):     ... 11 more

这些都是上面的代码和错误,我不明白为什么我在addTab中收到错误,而所有的语法都是我认为是正确的。

请告诉我,我如何在Fragment中创建FragmentTabhost,或者如何在片段中使用getTabHost()。

0 个答案:

没有答案