Android FragmentTabHost错误?

时间:2017-01-10 17:39:00

标签: android

当我执行应用程序时,我犯了这个错误;不幸的是,IDLEPOOR已经停止了。有人能找到问题告诉我吗?我在哪里弄错了。

activity_main.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"
    tools:context="com.natastudio.idlepoor.MainActivity">

    <android.support.v4.app.FragmentTabHost
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tabHost"
        android:layout_gravity="center_horizontal">

        <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">

                <include layout="@layout/page1"></include>
                <include layout="@layout/page2"></include>
                <include layout="@layout/page3"></include>
                <include layout="@layout/page4"></include>
                <include layout="@layout/page5"></include>
                <include layout="@layout/page6"></include>

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

MainActivity.java代码;

    package com.natastudio.idlepoor;

import android.content.res.Resources;
        import android.support.v4.app.FragmentTabHost;
        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.widget.TabHost;

public class MainActivity extends AppCompatActivity {

    private FragmentTabHost tabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        firstTabs();
    }

    private void firstTabs() {
        tabHost=(FragmentTabHost) findViewById(R.id.tabHost);
        tabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent );

        Resources res= getResources();

        TabHost.TabSpec tab1= tabHost.newTabSpec("tab1");
        TabHost.TabSpec tab2= tabHost.newTabSpec("tab2");
        TabHost.TabSpec tab3= tabHost.newTabSpec("tab3");
        TabHost.TabSpec tab4= tabHost.newTabSpec("tab4");
        TabHost.TabSpec tab5= tabHost.newTabSpec("tab5");
        TabHost.TabSpec tab6= tabHost.newTabSpec("tab6");

        tab1.setIndicator(res.getString(R.string.f1),null);
    tab2.setIndicator(res.getString(R.string.f2),null);
    tab3.setIndicator(res.getString(R.string.f3),null);
    tab4.setIndicator(res.getString(R.string.f4),null);
    tab5.setIndicator(res.getString(R.string.f5),null);
    tab6.setIndicator(res.getString(R.string.f6),null);

        tabHost.addTab(tab1, page1fragment.class, null);
        tabHost.addTab(tab2, page2fragment.class, null);
        tabHost.addTab(tab3, page3fragment.class, null);
        tabHost.addTab(tab4, page4fragment.class, null);
        tabHost.addTab(tab5, page5fragment.class, null);
        tabHost.addTab(tab6, page6fragment.class, null);
    }
}

其中一个片段代码;

    package com.natastudio.idlepoor;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class page1fragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.page1, container, false);
        TextView tv1=(TextView) v.findViewById(R.id.textView);

        Resources res = getResources();

        tv1.setText("Fragment1");

        return v;
    }
}

logcat的:CLICK

0 个答案:

没有答案