Question

I just copied the Navigation Drawer example from developer.android.com. The Drawer works and I can change Activity with it but the problem is that I can't see the fragment of my activity. What is my problem? Here's my code:

Activity.java:

public class Guide extends ActionBarActivity {

private String[] drawerListViewItems;
private DrawerLayout drawerLayout;
private ListView drawerListView;
private ActionBarDrawerToggle actionBarDrawerToggle;

boolean isPlayersOpen = false;
boolean isMatchOpen = false;
boolean isTournamentOpen = false;

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

    // get list items from strings.xml
    drawerListViewItems = getResources().getStringArray(R.array.activity_list);
    // get ListView defined in activity_main.xml
    drawerListView = (ListView) findViewById(R.id.left_drawer);

    // Set the adapter for the list view
    drawerListView.setAdapter(new ArrayAdapter<String>(this,
            R.layout.drawer_listview_item, drawerListViewItems));

    // 2. App Icon 
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    // 2.1 create ActionBarDrawerToggle
    actionBarDrawerToggle = new ActionBarDrawerToggle(
            this,                  /* host Activity */
            drawerLayout,         /* DrawerLayout object */
            R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
            R.string.drawer_open,  /* "open drawer" description */
            R.string.drawer_close  /* "close drawer" description */
            );

    // 2.2 Set actionBarDrawerToggle as the DrawerListener
    drawerLayout.setDrawerListener(actionBarDrawerToggle);

    // 2.3 enable and show "up" arrow
    getActionBar().setDisplayHomeAsUpEnabled(true); 

    // just styling option
    drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

    drawerListView.setOnItemClickListener(new DrawerItemClickListener());
}

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

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    actionBarDrawerToggle.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.guide, menu);
    return true;
}

public void extendPlayer(View view){

    ImageView extendPlayer = (ImageView)findViewById(R.id.players1);
    ImageView collapsePlayer = (ImageView)findViewById(R.id.players2);
    TextView players_text = (TextView)findViewById(R.id.players_guide_text);

    if(isPlayersOpen){

        extendPlayer.setVisibility(View.VISIBLE);
        collapsePlayer.setVisibility(View.GONE);
        players_text.setVisibility(View.GONE);
        isPlayersOpen = false;

    } else {

        extendPlayer.setVisibility(View.GONE);
        collapsePlayer.setVisibility(View.VISIBLE);
        players_text.setVisibility(View.VISIBLE);
        isPlayersOpen = true;

    }

}

public void extendMatch(View view){

    ImageView extendMatch = (ImageView)findViewById(R.id.match1);
    ImageView collapseMatch = (ImageView)findViewById(R.id.match2);
    TextView match_text = (TextView)findViewById(R.id.match_guide_text);

    if(isMatchOpen){

        extendMatch.setVisibility(View.VISIBLE);
        collapseMatch.setVisibility(View.GONE);
        match_text.setVisibility(View.GONE);
        isMatchOpen = false;

    } else {

        extendMatch.setVisibility(View.GONE);
        collapseMatch.setVisibility(View.VISIBLE);
        match_text.setVisibility(View.VISIBLE);
        isMatchOpen = true;

    }

}

public void extendTournament(View view){

    ImageView extendTournament = (ImageView)findViewById(R.id.tournament1);
    ImageView collapseTournament = (ImageView)findViewById(R.id.tournament2);
    TextView tournament_text = (TextView)findViewById(R.id.tournament_guide_text);

    if(isTournamentOpen){

        extendTournament.setVisibility(View.VISIBLE);
        collapseTournament.setVisibility(View.GONE);
        tournament_text.setVisibility(View.GONE);
        isTournamentOpen = false;

    } else {

        extendTournament.setVisibility(View.GONE);
        collapseTournament.setVisibility(View.VISIBLE);
        tournament_text.setVisibility(View.VISIBLE);
        isTournamentOpen = true;

    }

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Pass the event to ActionBarDrawerToggle, if it returns
    // true, then it has handled the app icon touch event
    if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
      return true;
    }
    // Handle your other action bar items...

    return super.onOptionsItemSelected(item);
}

private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        drawerLayout.closeDrawer(drawerListView);

        if(position == 0){

            Intent intent = new Intent(Guide.this, Players.class);
            startActivity(intent);

        }

        if(position == 1){

            Toast.makeText(Guide.this, ((TextView)view).getText(), Toast.LENGTH_SHORT).show();

        }

        if(position == 2){

            Intent intent = new Intent(Guide.this, Tournaments.class);
            startActivity(intent);

        }

        if(position == 3){



        }

        if(position == 4){

            Intent intent = new Intent(Guide.this, Info.class);
            startActivity(intent);

        }

    }
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

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

}

ExtendPlayer, ExtendMatch and ExtendTournament are my personal methods.

Activity.xml:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#ddd"/>

and my Fragments is called fragment_guide.xml

Thanks.

Was it helpful?

Solution

First of all, it's preferable if you don't start new activities on selecting the NavigationDrawer List items. What you should do, is just replace the existing fragment in the FrameLayout with the id contentframe. This has the benefit that it'll make it easier for you to scale your application later to tablets.

Regarding your question about fragments not getting instantiated, you have nothing in your FrameLayout in your Activity.xml. You would want to instantiate your fragments inside this FrameLayout. And after selecting the option from the NavigationDrawer List, just replace the fragment in the FrameLayout with the corresponding fragments.

Use FragmentTransaction to achieve this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top