下面是我的代码的一部分:

Fragment fragment = null;
        switch (position) {
        case 0:
            fragment = new HomeFragment();
            break;
        case 1:
            fragment = new FindPeopleFragment();
            break;
        case 2:
            fragment = new PhotosFragment();
            break;
        case 3:
            fragment = new CommunityFragment();
            break;
        case 4:
            fragment = new PagesFragment();
            break;
        case 5:
            fragment = new WhatsHotFragment();
            break;

        default:
            break;
        }

        if (fragment != null) {
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.frame_container, fragment).commit();

            // 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");
        }

我如何制作 HomeFragment ListView 或选项卡小部件。
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class HomeFragment extends Fragment {

    public HomeFragment(){}

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

        View rootView = inflater.inflate(R.layout.tabs_scroll, container, false);
        return rootView;
    }
}

如果我应该扩展 listfragment,则行:
fragmentManager.beginTransaction()
                        .replace(R.id.frame_container, fragment).commit();

标记错误:"The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, HomeFragment)"
请,我如何使这项工作,以及我如何仍然添加一个标签小部件假设我不想让它成为 ListView

最佳答案

试试这个..

你不能这样做 Fragment fragment = new HomeFragment(); 如果你这样做你会得到 Can not convert HomeFragment to Fragment 因为 HomeFragment 是 fragment 类所以你不能将它分配到 Fragment 。所以,你必须喜欢 HomeFragment fragment = new HomeFragment(); 例如从你的代码。

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

            if (fragment != null) {
                  FragmentManager fragmentManager = getSupportFragmentManager();
                  fragmentManager.beginTransaction()
                            .replace(R.id.frame_container, fragment).commit();

                  // 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");
            }
            break;

            // do like this for remaining cases

        default:
            break;
        }

10-07 19:40