【问题标题】:Android use FragmentActivity instead of TabActivityAndroid 使用 FragmentActivity 而不是 TabActivity
【发布时间】:2015-03-05 12:39:24
【问题描述】:

我想将TabActivity 更改为FragmentActivity,因为TabActivity 已被弃用。

我的代码是

public class Fragment_Activity extends TabActivity implements TabHost.OnTabChangeListener {

    /** Called when the activity is first created. */
    TabHost tabHost;

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

        // Get TabHost Refference
        tabHost = getTabHost();

        // Set TabChangeListener called when tab changed
        tabHost.setOnTabChangedListener(this);

        TabHost.TabSpec spec;
        Intent intent;

        /************* TAB1 ************/
        // Create  Intents to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, OccasionFragment.class);
        spec = tabHost.newTabSpec("First").setIndicator("")
                .setContent(intent);

        //Add intent to tab
        tabHost.addTab(spec);

        /************* TAB2 ************/
        intent = new Intent().setClass(this, InvitationFragment.class);
        spec = tabHost.newTabSpec("Second").setIndicator("")
                .setContent(intent);
        tabHost.addTab(spec);

        /************* TAB3 ************/
        intent = new Intent().setClass(this, FriendsFragment.class);
        spec = tabHost.newTabSpec("Third").setIndicator("")
                .setContent(intent);
        tabHost.addTab(spec);

        // Set drawable images to tab
        tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.ic_action_event);
        tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.ic_action_phone);

        // Set Tab1 as Default tab and change image
        tabHost.getTabWidget().setCurrentTab(0);
        tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.ic_action_person);


    }

    @Override
    public void onTabChanged(String tabId) {

        /************ Called when tab changed *************/

        //********* Check current selected tab and change according images *******/

        for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
        {
            if(i==0)
                tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.ic_action_event);
            else if(i==1)
                tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.ic_action_phone);
            else if(i==2)
                tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.ic_action_person);
        }


        if(tabHost.getCurrentTab()==0)
            tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.ic_action_event);
        else if(tabHost.getCurrentTab()==1)
            tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.ic_action_phone);
        else if(tabHost.getCurrentTab()==2)
            tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.ic_action_person);

    }`

它的工作,但我想改变FragmentActivity

Android 开发者链接:TabActivity

当我使用FragmentActivity 时不起作用 请帮帮我。

【问题讨论】:

    标签: android android-fragments android-activity android-tabhost android-fragmentactivity


    【解决方案1】:

    替换

     tabHost = getTabHost();
    

    tabHost = (TabHost)findViewById(android.R.id.tabhost);
    tabHost.setup();
    

    这将适用于FragmentActivity,假设所有其他事情都是正确的。

    【讨论】:

    • 不,我在 tabHost = (TabHost)findViewById(android.R.id.tabhost); 中得到空指针异常;
    • 发布您的 XML 布局文件 fragment_tabs.xml
    【解决方案2】:

    fragment_tabs.xml

    <?xml version="1.0" encoding="utf-8"?>
    

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <include layout="@layout/header_layout" />
    
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" />
    
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="-4dp"
            android:layout_weight="0" />
    </LinearLayout>
    

    fragment_activity.java

    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentTabHost;
    
    public class Fragment_Activity extends FragmentActivity {
    
    /** Called when the activity is first created. */
    private static final String TAB_1_TAG = "Invitation";
    private static final String TAB_2_TAG = "Occasion";
    private static final String TAB_3_TAG = "Friends";
    private FragmentTabHost tabHost;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_tabs);
    
        // Get TabHost Refference
        tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        tabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
    
        tabHost.addTab(tabHost.newTabSpec(TAB_1_TAG).setIndicator(""), InvitationFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec(TAB_2_TAG).setIndicator(""), OccasionFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec(TAB_3_TAG).setIndicator(""), ContactActivity.class, null);
    
        // Set drawable images to tab
        tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.ic_action_event);
        tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.ic_action_phone);
    
        // Set Tab1 as Default tab and change image
        tabHost.getTabWidget().setCurrentTab(0);
        tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.ic_action_person);
    
    }
    

    }

    invitation_tab.xml

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    public class InvitationFragment extends Fragment {
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View V = inflater.inflate(R.layout.invitation_tab, container, false);
    
        return V;
    }
    

    }

    终于修复了tab_activity问题。 感谢您帮助@ZygoteInit..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多