web-dev-qa-db-ja.com

Androidのフラグメント内にタブを追加しますか?

Fragment内にTabHostを追加しようとしています。以下にコードを示します。ここフラグメントの中。 TabHostを追加して2つのタブを表示しようとしています。

package com.nordicsoft.dilosysNewVersion;

import Android.app.Fragment;
import Android.content.Intent;
import Android.os.Bundle;
import Android.view.LayoutInflater;
import Android.view.View;
import Android.view.ViewGroup;
import Android.widget.TabHost;

public class Fragment_Coupons extends Fragment {
    TabHost tabHost;
    TabHost.TabSpec spec;

    public Fragment_Coupons() {
    }

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

        View rootView = inflater.inflate(R.layout.fragment_coupon, container,
                false);
        tabHost = (TabHost) rootView.findViewById(Android.R.id.tabhost);
        tabHost.setup();
        //Add_Text_To_Tab("Android", "IOS");

        Intent intentAndroid = new Intent().setClass(getActivity(),
                Aclass.class);
        spec = tabHost.newTabSpec("Android").setContent(intentAndroid)
                .setIndicator("Android");

        tabHost.addTab(spec);

        Intent intentBus = new Intent().setClass(getActivity(), Bclass.class);
        spec = tabHost.newTabSpec("Welcome").setIndicator("Welcome")
                .setContent(intentBus).setIndicator("Welcome");
        tabHost.addTab(spec);
        return rootView;
    }

}

XMLコード:

<?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="fill_parent"
    Android:layout_height="fill_parent">
    <LinearLayout
        Android:orientation="vertical"
        Android:layout_width="fill_parent"
        Android:layout_height="fill_parent">
        <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"/>
    </LinearLayout>
</TabHost>

これは私にエラーを与えています:

12-09 17:40:53.509: E/AndroidRuntime(18103): FATAL EXCEPTION: main
12-09 17:40:53.509: E/AndroidRuntime(18103): Java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
12-09 17:40:53.509: E/AndroidRuntime(18103):    at Android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.Java:690)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at Android.widget.TabHost.setCurrentTab(TabHost.Java:356)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at Android.widget.TabHost.addTab(TabHost.Java:246)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at com.nordicsoft.dilosysNewVersion.Fragment_Coupons.onCreateView(Fragment_Coupons.Java:32)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at Android.app.FragmentManagerImpl.moveToState(FragmentManager.Java:828)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at Android.app.FragmentManagerImpl.moveToState(FragmentManager.Java:1032)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at Android.app.BackStackRecord.run(BackStackRecord.Java:622)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at Android.app.FragmentManagerImpl.execPendingActions(FragmentManager.Java:1382)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at Android.app.FragmentManagerImpl$1.run(FragmentManager.Java:426)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at Android.os.Handler.handleCallback(Handler.Java:605)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at Android.os.Handler.dispatchMessage(Handler.Java:92)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at Android.os.Looper.loop(Looper.Java:137)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at Android.app.ActivityThread.main(ActivityThread.Java:4517)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at Java.lang.reflect.Method.invokeNative(Native Method)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at Java.lang.reflect.Method.invoke(Method.Java:511)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:993)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:760)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at dalvik.system.NativeStart.main(Native Method)
12-09 17:46:02.770: I/Process(18103): Sending signal. PID: 18103 SIG: 9
36
NRahman

タブを処理するためにこれを試してみてください:

public class MainFragment extends Fragment {
    private FragmentTabHost mTabHost;

    //Mandatory Constructor
    public MainFragment() {
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

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

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


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

        mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("Fragment B"),
                FragmentB.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("Fragment C"),
                FragmentC.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("fragmentd").setIndicator("Fragment D"),
                FragmentD.class, null);


        return rootView;
    }
}

レイアウトで:

<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/realtabcontent"
            Android:layout_width="match_parent"
            Android:layout_height="0dp"
            Android:layout_weight="1"/>

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

MainFragmentをホストするMotherActivity:

public class MotherActivity extends FragmentActivity {

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

        MainFragment fragmenttab = new MainFragment();
        getSupportFragmentManager().beginTransaction()
        .add(R.id.item_detail_container, fragmenttab).commit();


    }

MotherActivityレイアウト:

<FrameLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    xmlns:tools="http://schemas.Android.com/tools"
    Android:id="@+id/item_detail_container"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
 />

その後、通常のフラグメントBおよびCなどのクラスを作成します。結果は次のようになります。

Tab Fragment inside Fragment Result

68
ElCharro

この行を追加してみてください

tabHost.setup(this.getActivity().getLocalActivityManager());

行の前

tabHost.addTab(spec);

または追加してみてください:

LocalActivityManager mLocalActivityManager = new LocalActivityManager(this.getActivity(), false);
mLocalActivityManager.dispatchCreate(savedInstanceState);
Host.setup(mLocalActivityManager);
1
SalGad