当第二次选择片段时,显示空白屏幕。

4
在我的应用程序中,我使用了SherlockNavigationDrawer。默认情况下,navigationDrawer有3个菜单打开3个片段。我已修改以下其中一个片段。但是,在第二次点击我的片段的菜单时,屏幕不显示任何内容!第一次没有问题;但是第二次会显示空白页面。
在这种情况下,如果在NavigationDrawer中单击默认片段菜单,然后再单击我的片段,屏幕会回来,但是如果再次单击我的片段菜单,则屏幕会显示空白页!
此外,我在我的片段中显示了一个项目列表,通过点击每个项目替换新的片段,以显示特定项目的详细信息。但详细片段是一个空的显示!!!
朋友们!问题出在哪里?
MainActivity 代码:
public class ActivityMain extends SherlockFragmentActivity {

// Declare Variables
DrawerLayout mDrawerLayout;
ListView mDrawerList;
ActionBarDrawerToggle mDrawerToggle;
MenuListAdapter mMenuAdapter;
String[] title;
String[] subtitle;
int[] icon;
Fragment fragment1 = new Fragment1();
Fragment fragment2 = new Fragment2();
Fragment fragment_categorylist = new Fragment_CategoryList();
private CharSequence mDrawerTitle;
private CharSequence mTitle;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from drawer_main.xml
    setContentView(R.layout.drawer_main);

    // Get the Title
    mTitle = mDrawerTitle = getTitle();

    // Generate title
    title = new String[] { getString(R.string.TitleFragment1), 
            getString(R.string.TitleFragment2),
            getString(R.string.TitleFragment3) };

    // Generate subtitle
    subtitle = new String[] { getString(R.string.SubtitleFragment1),
            getString(R.string.SubtitleFragment2),
            getString(R.string.SubtitleFragment3) };

    // Generate icon
    icon = new int[] { R.drawable.action_about, R.drawable.action_settings,
            R.drawable.collections_cloud };

    // Locate DrawerLayout in drawer_main.xml
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    // Locate ListView in drawer_main.xml
    mDrawerList = (ListView) findViewById(R.id.listview_drawer);

    // Set a custom shadow that overlays the main content when the drawer
    // opens
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
            GravityCompat.START);

    mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);

    ActivitySwipeDetector swipe = new ActivitySwipeDetector(this);
    DrawerLayout swipe_layout = (DrawerLayout) findViewById(R.id.drawer_layout);
    swipe_layout.setOnTouchListener(swipe);

    //LinearLayout swipe_layout2 = (LinearLayout) findViewById(R.id.fragment_pager_layout);
    //swipe_layout2.setOnTouchListener(swipe);

    // Pass string arrays to MenuListAdapter
    mMenuAdapter = new MenuListAdapter(ActivityMain.this, title, subtitle,
            icon);

    // Set the MenuListAdapter to the ListView
    mDrawerList.setAdapter(mMenuAdapter);

    // Capture listview menu item click
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    // Enable ActionBar app icon to behave as action to toggle nav drawer
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    // ActionBarDrawerToggle ties together the the proper interactions
    // between the sliding drawer and the action bar app icon
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, R.string.drawer_open,
            R.string.drawer_close) {

        public void onDrawerClosed(View view) {
            // TODO Auto-generated method stub
            super.onDrawerClosed(view);
        }

        public void onDrawerOpened(View drawerView) {
            // TODO Auto-generated method stub
            // Set the title on the action when drawer open
            getSupportActionBar().setTitle(mDrawerTitle);
            super.onDrawerOpened(drawerView);
        }
    };

    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) {
        selectItem(0);
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (item.getItemId() == android.R.id.home) {

        if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            mDrawerLayout.openDrawer(mDrawerList);
        }
    }

    return super.onOptionsItemSelected(item);
}

// ListView click listener in the navigation drawer
private class DrawerItemClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        selectItem(position);
    }
}

private void selectItem(int position) {

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    // Locate Position
    switch (position) {
    case 0:
        ft.replace(R.id.content_frame, fragment1);
        break;
    case 1:
        ft.replace(R.id.content_frame, fragment2);
        break;
    case 2:
        ft.replace(R.id.content_frame, fragment_categorylist);
        break;
    }
    ft.commit();
    mDrawerList.setItemChecked(position, true);

    // Get the title followed by the position
    setTitle(title[position]);
    // Close drawer
    mDrawerLayout.closeDrawer(mDrawerList);
}

默认片段代码:

public class Fragment3 extends SherlockFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment3, container, false);
    return rootView;
}

我的Fragment(修改的Fragment):

public class Fragment_CategoryList extends SherlockFragment {

static final int NUM_ITEMS = 1;

MyAdapter mAdapter;

ViewPager mPager;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_pager, container,
            false);

    mAdapter = new MyAdapter(getSherlockActivity().getSupportFragmentManager());

    mPager = (ViewPager) rootView.findViewById(R.id.pager);

    mPager.setAdapter(mAdapter);

    // Watch for button clicks.
    Button button = (Button) rootView.findViewById(R.id.goto_first);
    button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            mPager.setCurrentItem(0);
        }
    });
    button = (Button) rootView.findViewById(R.id.goto_last);
    button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            mPager.setCurrentItem(NUM_ITEMS - 1);
        }
    });

    return rootView;
}


// ////////////////////////////////////////////////////////////////////////

public static class MyAdapter extends FragmentPagerAdapter {
    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {
        return NUM_ITEMS;
    }

    @Override
    public Fragment getItem(int position) {
        return ArrayListFragment.newInstance(position);
    }
}

public static class ArrayListFragment extends SherlockListFragment {
    int mNum;

    /**
     * Create a new instance of CountingFragment, providing "num" as an
     * argument.
     */
    static ArrayListFragment newInstance(int num) {
        ArrayListFragment f = new ArrayListFragment();

        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putInt("num", num);
        f.setArguments(args);

        return f;
    }

    /**
     * When creating, retrieve this instance's number from its arguments.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mNum = getArguments() != null ? getArguments().getInt("num") : 1;
    }

    /**
     * The Fragment's UI is just a simple text view showing its instance
     * number.
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_pager_list, container,
                false);
        View tv = v.findViewById(R.id.text);
        ((TextView) tv).setText("Fragment #" + mNum);
        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        // setListAdapter(new ArrayAdapter<String>(getActivity(),
        // android.R.layout.simple_list_item_1, Cheeses.sCheeseStrings));

        DBAdapter db = new DBAdapter(getActivity());

        // Create an array to specify the fields we want to display in the
        // list (only TITLE)
        String[] from = new String[] { "title", "_id" };

        // and an array of the fields we want to bind those fields to (in
        // this case just text1)
        int[] to = new int[] { R.id.entry1, R.id.entry2 };

        ArrayList<HashMap<String, String>> categoryList = db.getAll();

        if (categoryList.size() != 0) {

            ListAdapter adapter = new SimpleAdapter(getActivity(),
                    categoryList,
                    R.layout.fragment_pager_list_entry, from, to);
            setListAdapter(adapter);
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {

        final FragmentTransaction ft = getSherlockActivity().getSupportFragmentManager().beginTransaction();

        Fragment myFragment = new Fragment_QA();

        int idplus = (int)id + 1;
        Bundle args = new Bundle();
        args.putLong("cid", idplus);
        //myFragment.setArguments(args);

        ft.replace(((ViewGroup)(getView().getParent())).getId(), myFragment);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.addToBackStack(null);
        ft.commit();
    }
}

}

主活动用户界面:

<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<ListView
    android:id="@+id/listview_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#111"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp" />

我的Fragment UI(Fragment_CategoryList):

<LinearLayout
    android:orientation="vertical" android:padding="4dip"
    android:gravity="center_horizontal"
    android:layout_width="match_parent" 
    android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1">
</android.support.v4.view.ViewPager>
<LinearLayout android:orientation="horizontal"
        android:gravity="center" android:measureWithLargestChild="true"
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:layout_weight="0">
    <Button android:id="@+id/goto_first"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="FIRST">
    </Button>
    <Button android:id="@+id/goto_last"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="LAST">
    </Button>
</LinearLayout>


你的抽屉列表(drawerList)大小是否大于0,第二次呢? - Hardik
嗨,Mohammed,你找到解决方法了吗?我遇到了一个类似的问题。 - Ibrahim Yildirim
2个回答

1
我不知道这个问题是否还有答案,但对某些人来说可能是一个有用的答案。
你应该这样做:
android.support.v4.app.FragmentPagerAdapter更改为android.support.v4.app.FragmentStatePagerAdapter

0
尝试将getSupportFragmentManager更改为getChildFragmentManager。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接