Android Studio中的FragmentTabHost

时间:2015-08-25 13:54:10

标签: android android-fragments android-tabhost

我正在尝试在我的应用中实施3标签系统。

Index.java(应该找到3个标签)

public class Index extends FragmentActivity{
private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_index);
    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(),         android.R.id.tabcontent);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab1").setIndicator("Tab 1", null),
            FragmentTab.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab2").setIndicator("Tab 2", null),
            FragmentTab.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab3").setIndicator("Tab 3", null),
            FragmentTab.class, null);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_index,menu);

    return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

INDEX.XML

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.vincent.tennispartner2.Index">

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

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"/>

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

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

FragmentTab.java

/**
 * Created by Vincent on 2015-08-25.
 */
public class FragmentTab extends Fragment {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_layout, container, false);
    TextView tv = (TextView) v.findViewById(R.id.text);
    tv.setText(this.getTag() + " Content");
    return v;
}
}

我已经被困在这个部分好几个小时,从动作栏到碎片到工具栏到其他东西,但我找不到任何适合我的东西。看到我刚刚开始在java / android中编码,我真的可以使用一些帮助。

完全错误:

08-25 15:41:22.085  13008-13008/com.example.vincent.tennispartner2     E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.vincent.tennispartner2, PID: 13008
    java.lang.RuntimeException: Unable to start activity     ComponentInfo{com.example.vincent.tennispartner2/com.example.vincent.tennispartn    er2.Index}: 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         android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
                at       android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2760)
                at     android.app.ActivityThread.access$900(ActivityThread.java:177)
                at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:145)
                at android.app.ActivityThread.main(ActivityThread.java:5944)
                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:1389)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
     Caused by: 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.example.vincent.tennispartner2.Index.onCreate(Index.java:25)
                at android.app.Activity.performCreate(Activity.java:6289)
                at     android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2760)
                at android.app.ActivityThread.access$900(ActivityThread.java:177)
                at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:145)
                at android.app.ActivityThread.main(ActivityThread.java:5944)
                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:1389)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)

非常感谢任何提示!

2 个答案:

答案 0 :(得分:0)

更改:

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

致:

.interview-patient {
    width: 100vw * $interview-patient-scalar; 
    height: 281.9vw * $interview-patient-scalar;
    max-height: 100vh * $interview-patient-scalar;
    max-width: 36.75vh * $interview-patient-scalar;
    margin: auto;
    position: absolute;
    top:0;bottom:0; /* vertical center */
    left:0;right:0; /* horizontal center */
}

答案 1 :(得分:0)

变化:

setContentView(R.layout.activity_index);

要:

setContentView(R.layout.index);