So right now I am working on an Android app and I am trying to switch to different activities from the NavigationDrawer. In my MainActivity, I have the nav drawer set up, and a StartingFragment that acts as the main view/activity when the app is first open.
However, I am unable to do this. Whenever I click on any of the list items that I have in my nav drawer, there is no switching of activities. I have looked across StackOverflow and other websites for the solution to my problem, but I can't seem to figure out the issue.
public class MainActivity extends FragmentActivity {
Button tea_type;
private String[] navDrawerTitles;
private DrawerLayout navDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mTitle;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navDrawerTitles = getResources().getStringArray(R.array.nav_array);
navDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, navDrawerTitles));
// When a menu item is clicked
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long stuff) {
final FragmentManager manager = getSupportFragmentManager();
final FragmentTransaction fragmentTransaction = manager.beginTransaction();
Fragment frag;
// Depending on position, set your fragment
if (position == 0) {
frag = new StartingFragment();
fragmentTransaction.add(R.id.starting_fragment, frag);
fragmentTransaction.addToBackStack(null);
// "Commits" the fragment to the fragment view in the layout
fragmentTransaction.commit();
}
}
});
navDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
navDrawerLayout, /* 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 */
) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getActionBar().setTitle(R.string.app_name);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActionBar().setTitle(R.string.app_name);
}
};
// Set the drawer toggle as the DrawerListener
navDrawerLayout.setDrawerListener(mDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
}
/* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content
// view
boolean drawerOpen = navDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class DrawerItemClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
selectItem(position);
}
}
/** Swaps fragments in the main content view */
/** Starts an Activity when item is clicked*/
private void selectItem(int position) {
switch (position) {
case 0:
Intent i1 = new Intent(MainActivity.this, setSelfTimeActivity.class);
this.startActivity(i1);
break;
}
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
@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);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
}
I think that the problem may be with this method, because everywhere I have looked there have been comments about the selectItem method. I don't see anything wrong though.
/** Swaps fragments in the main content view */
/** Starts an Activity when item is clicked*/
private void selectItem(int position) {
switch (position) {
case 0:
Intent i1 = new Intent(MainActivity.this, setSelfTimeActivity.class);
startActivity(i1);
break;
}
}