片段未显示

Fragment not showing

所以我一直在根据我在教程中找到的代码构建我的导航抽屉。但是我的片段不会切换。笔记本电脑已关闭并正在使用 AIDE。谁能告诉我怎么了?猜猜这是我的 setonitemclicklistener。

{

private ListView mDrawerList;
private DrawerLayout mDrawerLayout;
private ArrayAdapter<String> mAdapter;
private ActionBarDrawerToggle mDrawerToggle;
private String mActivityTitle;

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

    mDrawerList = (ListView)findViewById(R.id.navList);mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    mActivityTitle = getTitle().toString();

    addDrawerItems();
    setupDrawer();

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
}

private void addDrawerItems()
{
    String[] osArray = { "Hello", "Watch Ads", "TapJoy" };
    mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
    mDrawerList.setAdapter(mAdapter);

    mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                selectView(position);
            }
        });
}

private void selectView(int position)
{
    Fragment fragment = null;

    switch (position)
    {
        case 0: 
            fragment = new HelloFragment();
            break;
        case 1 :
            fragment = new AdFragment();
            break;
        case 2:
            fragment = new TapJoyFragment();
            break;

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

        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        mDrawerLayout.closeDrawer(mDrawerList);
    }   }

private void setupDrawer()
{
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView)
        {
            super.onDrawerOpened(drawerView);
            getSupportActionBar().setTitle("How will you earn?");
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view)
        {
            super.onDrawerClosed(view);
            getSupportActionBar().setTitle(mActivityTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };

    mDrawerToggle.setDrawerIndicatorEnabled(true);
    mDrawerLayout.setDrawerListener(mDrawerToggle);
}

@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);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings)
    {

        return true;
    }

    // Activate the navigation drawer toggle
    if (mDrawerToggle.onOptionsItemSelected(item))
    {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

你好伙计!

private void selectView(int position)
        {
            Fragment fragment = null;

            switch (position)
            {
                case 0: 
                    fragment = new HelloFragment();
                    break;
                case 1 :
                    fragment = new AdFragment();
                    break;
                case 2:
                    fragment = new TapJoyFragment();
                    break;

            }   
            **if (fragment == null)**
            {
                FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

                mDrawerList.setItemChecked(position, true);
                mDrawerList.setSelection(position);
                mDrawerLayout.closeDrawer(mDrawerList);
            }   }

This "if" statement always be false 如果你在 switch(position).所以你的碎片交易永远不会实现。

首先取 FragmentActivity 而不是 Activity...

private void selectView(int position) { 片段 fragment = null;

switch (position)
{
    case 0: 
        fragment = new HelloFragment();
getSupportFragmentManager()
 .beginTransaction()

.replace(R.id.content_frame,fragment).commit();

        break;

    case 1 :

        fragment = new AdFragment();
getSupportFragmentManager()
 .beginTransaction()
.replace(R.id.content_frame,fragment).commit();

        break;

    case 2:

        fragment = new TapJoyFragment();
getSupportFragmentManager()
 .beginTransaction()
.replace(R.id.content_frame,fragment).commit();

        break;

}   

请使用 if(fragment != null) 而不是 (fragment == null)。