所以现在我正在开发Android应用程序,我正在尝试从NavigationDrawer切换到不同的活动。在我的MainActivity中,我设置了导航抽屉,以及在首次打开应用程序时充当主视图/活动的StartingFragment。
但是,我无法做到这一点。每当我点击导航抽屉中的任何列表项时,都不会切换活动。我查看了StackOverflow和其他网站以找到我的问题的解决方案,但我似乎无法弄清楚这个问题。
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);
}
}
我认为问题可能出在这个方法上,因为我看过的每个地方都有关于selectItem方法的评论。我没有看到任何错误。
/** 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;
}
}