带有 NavigationDrawer 的片段中的 TabHost

TabHost in a fragment with NavigationDrawer

好吧,我正在尝试在 Fragment 中创建一个 TabHostFragment,我面临的问题是我无法实现它,因为我已经在使用抽屉了导航。我在 SO 中寻找一些答案,但我没有找到最好的方法。 我正在使用这个 Navigation drawer example, but I've added more fragments and more items, the thing is that I tried to implement this code of this example,但它不起作用。我知道这是可能的,因为 Google Play, YouTube, Play Music... 所有这些应用程序都有一个抽屉导航,里面有标签。

问题是我尝试过的所有事情之一就是创建一个 TabHostFragment 适配器,就像在答案示例中那样,然后在当时这样做:

private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;

    switch (position) {
    case 0:
        mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);


        mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab1"),
                MisOfertasFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab2"),
                RecomendacionesFragment.class, null);
        break;

我不知道这样做是否正确,但我试过了,在这种情况下,它控制点击导航抽屉的哪个项目,然后当用户点击第一个项目时,它应该显示TabHost.

LogCat 错误看起来像:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.widget.TabHost$TabSpec android.support.v4.app.FragmentTabHost.newTabSpec(java.lang.String)' on a null object reference

此错误所指向的行是:

 mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab1"),

并在 displayView(0) 中:

    if (savedInstanceState == null) {
        // on first time display view for first nav item
        displayView(0);
    }

我知道这不是使用 FragmentTabHost 的最佳主意(在 switch 案例上实现它),但我也尝试创建一个新的 class 就像答案的例子和 cann 在这种情况下 fragment = new ClassWithTabs 它给了我同样的错误。

请告诉我我误解或做错了什么,因为我不明白我做错了什么。

如果你们需要更多代码,可以问我,我会尽快 post。

编辑

我已经更改了导入,我有 import android.app.FragmentManager; 并且我更改为 import android.support.v4.app.FragmentManager;,并且只给我在这一堆代码中的一个错误:

if (fragment != null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.frame_container, fragment).commit(); //here's the error

            // update selected item and title, then close the drawer
            mDrawerList.setItemChecked(position, true);
            mDrawerList.setSelection(position);
            setTitle(navMenuTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);

错误 --> Error:(253, 37) error: incompatible types: android.app.Fragment cannot be converted to android.support.v4.app.Fragment

EDIT2

package info.androidhive.slidingmenu;
import info.androidhive.slidingmenu.adapter.NavDrawerListAdapter;
import info.androidhive.slidingmenu.model.NavDrawerItem;
import java.util.ArrayList;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

    public class MainActivity extends FragmentActivity {
        private DrawerLayout mDrawerLayout;
        private ListView mDrawerList;
        private ActionBarDrawerToggle mDrawerToggle;
        // saber si esta abierto
        public boolean mDrawerOpened;
        // nav drawer title
        private CharSequence mDrawerTitle;

        // used to store app title
        private CharSequence mTitle;

        //para ponerlo visible
        public MenuItem mi;

        // slide menu items
        private String[] navMenuTitles;
        private TypedArray navMenuIcons;

        private ArrayList<NavDrawerItem> navDrawerItems;
        private NavDrawerListAdapter adapter;

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


            mTitle = mDrawerTitle = getTitle();

            // load slide menu items
            navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

            // nav drawer icons from resources
            navMenuIcons = getResources()
                    .obtainTypedArray(R.array.nav_drawer_icons);

            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            mDrawerList = (ListView) findViewById(R.id.list_slidermenu);

            navDrawerItems = new ArrayList<NavDrawerItem>();

            // adding nav drawer items to array
            // Home
            navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
            // Find People
            navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
            // Photos
            navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
            // Communities, Will add a counter here
            navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
            // Pages
            navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
            // What's hot, We  will add a counter here
            navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));
            //AyudaSugerencias
            navDrawerItems.add(new NavDrawerItem(navMenuTitles[6], navMenuIcons.getResourceId(6, -1)));


            // Recycle the typed array
            navMenuIcons.recycle();

            mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

            // setting the nav drawer list adapter
            adapter = new NavDrawerListAdapter(getApplicationContext(),
                    navDrawerItems);
            mDrawerList.setAdapter(adapter);

            // enabling action bar app icon and behaving it as toggle button

            getActionBar().setDisplayHomeAsUpEnabled(true);
            getActionBar().setBackgroundDrawable(new ColorDrawable(0xff1d97dd));
            getActionBar().setHomeButtonEnabled(true);

            mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                    R.drawable.ic_drawer, //nav menu toggle icon
                    R.string.app_name, // nav drawer open - description for accessibility
                    R.string.app_name // nav drawer close - description for accessibility
            ) {
                public void onDrawerClosed(View view) {
                    getActionBar().setTitle(
                            Html.fromHtml("<font color='ffffff'>"
                                    + mTitle + "</font>"));

                    // calling onPrepareOptionsMenu() to show action bar icons
                    invalidateOptionsMenu();
                    mDrawerOpened = false;
                    syncState();

                }

                public void onDrawerOpened(View drawerView) {
                    getActionBar().setTitle(
                            Html.fromHtml("<font color='ffffff'>"
                                    + mDrawerTitle + "</font>"));

                    // calling onPrepareOptionsMenu() to hide action bar icons
                    invalidateOptionsMenu();
                    mDrawerOpened = true;
                    syncState();
                }
            };
            mDrawerLayout.setDrawerListener(mDrawerToggle);

            if (savedInstanceState == null) {
                // on first time display view for first nav item
                displayView(0);
            }
        }

        /**
         * Slide menu item click listener
         * */
        private class SlideMenuClickListener implements
                ListView.OnItemClickListener {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                // display view for selected nav drawer item
                displayView(position);
            }
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {


            getMenuInflater().inflate(R.menu.main, menu);


            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            Fragment fragment = null;
            // toggle nav drawer on selecting action bar app icon/title
            if (mDrawerToggle.onOptionsItemSelected(item)) {
                return true;
            }
            // Handle action bar actions click
            switch (item.getItemId()) {
            case R.id.action_settings:
                return true;
                case R.id.ofertasRefresh:
                    return true;
                case R.id.menu_search:
                    return true;
                case R.id.newOffer:
                    getFragmentManager().beginTransaction().replace(R.id.frame_container, new TipusNouProducte()).commit();
                    return true;
            default:
                return super.onOptionsItemSelected(item);
            }
        }

        /* *
         * Called when invalidateOptionsMenu() is triggered
         */
        @Override
        public boolean onPrepareOptionsMenu(Menu menu) {
            // if nav drawer is opened, hide the action items
            boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
            menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
            if (mDrawerOpened) { menu.removeItem(R.id.ofertasRefresh);
            menu.removeItem(R.id.menu_search);menu.removeItem(R.id.newOffer);}
            if (!mDrawerOpened) { menu.add(Menu.NONE, R.id.ofertasRefresh, Menu.NONE, mTitle); }
            return super.onPrepareOptionsMenu(menu);

        }

        /**
         * Diplaying fragment view for selected nav drawer list item
         * */
        private void displayView(int position) {
            //Fragment fragment = null;  ---Removed for test, because if I don't remove this line couldn't execute the app because fragment can not call a FragmentActivity....
            FragmentActivity fragment = null;

            switch (position) {
                case 0:
                    fragment = new TabHostFragment();
                    break;

                default:
                break;


                if (fragment != null) {
                    FragmentManager fragmentManager = getFragmentManager();
                    fragmentManager.beginTransaction()
                            .replace(R.id.frame_container, fragment).commit(); /// here says that replace android.app.Fragment in FragmentTransaction cannot be applied...

                    // update selected item and title, then close the drawer
                    mDrawerList.setItemChecked(position, true);
                    mDrawerList.setSelection(position);
                    setTitle(navMenuTitles[position]);
                    mDrawerLayout.closeDrawer(mDrawerList);
                } else {
                    // error in creating fragment
                    Log.e("MainActivity", "Error in creating fragment");
                }
            }
        }

        @Override
        public void setTitle(CharSequence title) {
            mTitle = title;
            getActionBar().setTitle(mTitle);

        }

        /**
         * When using the ActionBarDrawerToggle, you must call it during
         * onPostCreate() and onConfigurationChanged()...
         */

        @Override
        protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            // Sync the toggle state after onRestoreInstanceState has occurred.
            mDrawerToggle.syncState();
        }

        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            // Pass any configuration change to the drawer toggls
            mDrawerToggle.onConfigurationChanged(newConfig);
        }



    }

这是错误

这是TabHostFragment.class

    package info.androidhive.slidingmenu;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;

/**
 * Created by Joan on 26/02/2015.
 */
public class TabHostFragment extends FragmentActivity {
    // Fragment TabHost as mTabHost
    private FragmentTabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabhost);

        mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab1"),
                MisOfertasFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab2"),
                RecomendacionesFragment.class, null);

    }
}

EDIT3

 private void displayView(int position) {
    //Fragment fragment = null;  ---Removed for test, because if I don't remove this line couldn't execute the app because fragment can not call a FragmentActivity....

    if(position == 0){

        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
        setContentView(R.layout.tabhost);

        mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Mis ofertas"),
                MisOfertasFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Recomendaciones"),
                RecomendacionesFragment.class, null);
    }
    else
    {
        Fragment fragment = null;

        switch (position) {
            case 1:
                fragment = new RecomendacionesFragment();
                break;
            case 2:
                fragment = new LocalizacionFragment();
                break;
            case 3:
                fragment = new ListaProductosFragment();
                break;
            case 4:
                fragment = new ConfiguracionFragment();
                break;
            case 5:
                fragment = new AyudaSugerenciasFragment();
                break;
            case 6:
                fragment = new AyudaSugerencias();
                break;

            default:
                break;
        }


        if (fragment != null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.frame_container, fragment).commit(); /// here says that replace android.app.Fragment in FragmentTransaction cannot be applied...

            // update selected item and title, then close the drawer
            mDrawerList.setItemChecked(position, true);
            mDrawerList.setSelection(position);
            setTitle(navMenuTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            // error in creating fragment
            Log.e("MainActivity", "Error in creating fragment");
        }
    }
}

EDIT4 LogCat错误

Process: info.androidhive.slidingmenu, PID: 2495
java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.slidingmenu/info.androidhive.slidingmenu.MainActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {info.androidhive.slidingmenu/info.androidhive.slidingmenu.TabHostFragment}; have you declared this activity in your AndroidManifest.xml?
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
    at android.app.ActivityThread.access0(ActivityThread.java:144)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5221)
    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:899)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {info.androidhive.slidingmenu/info.androidhive.slidingmenu.TabHostFragment}; have you declared this activity in your AndroidManifest.xml?
    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1761)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1485)
    at android.app.Activity.startActivityForResult(Activity.java:3736)
    at android.app.Activity.startActivityForResult(Activity.java:3697)
    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:817)
    at android.app.Activity.startActivity(Activity.java:4007)
    at android.app.Activity.startActivity(Activity.java:3975)
    at info.androidhive.slidingmenu.MainActivity.displayView(MainActivity.java:221)
    at info.androidhive.slidingmenu.MainActivity.onCreate(MainActivity.java:138)
    at android.app.Activity.performCreate(Activity.java:5933)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
    at android.app.ActivityThread.access0(ActivityThread.java:144)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5221)
    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:899)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

您忘记打电话了

mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

调用 findViewById().

后立即在 displayView() 方法中添加以上内容

编辑:

1.FragmentActivity 而不是 Activity.

扩展你的 MainActivity

2. 确保您的 FragmentManager 被导入为

import android.support.v4.app.FragmentManager;

而不是

import android.app.FragmentManager;

编辑 2:

确保您的 Fragment 导入为

import android.support.v4.app.Fragment;

而不是

import android.app.Fragment;

编辑 3:

您的 displayView() 方法应该是:

private void displayView(int position) {
        //Fragment fragment = null;  ---Removed for test, because if I don't remove this line couldn't execute the app because fragment can not call a FragmentActivity....

    if(position == 0){

        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
        Intent intent = new Intent(MainActivity.this, TabHostFragment.class);
        startActivity(intent);
    }
    else
    {
        Fragment fragment = null;

        switch (position) {
        case 1:
            fragment = new RecomendacionesFragment();
            break;
        case 2:
            fragment = new LocalizacionFragment();
            break;
        case 3:
            fragment = new ListaProductosFragment();
            break;
        case 4:
            fragment = new ConfiguracionFragment();
            break;
        case 5:
            fragment = new AyudaSugerenciasFragment();
            break;
            case 6:
                fragment = new AyudaSugerencias();
                break;

            default:
            break;
         }


            if (fragment != null) {
                FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction()
                        .replace(R.id.frame_container, fragment).commit(); /// here says that replace android.app.Fragment in FragmentTransaction cannot be applied...

                // update selected item and title, then close the drawer
                mDrawerList.setItemChecked(position, true);
                mDrawerList.setSelection(position);
                setTitle(navMenuTitles[position]);
                mDrawerLayout.closeDrawer(mDrawerList);
            } else {
                // error in creating fragment
                Log.e("MainActivity", "Error in creating fragment");
            }
        }
        }
    }