Android 安卓:点击操作栏无法打开导航抽屉

Android 安卓:点击操作栏无法打开导航抽屉,android,android-actionbar,navigation-drawer,Android,Android Actionbar,Navigation Drawer,从和各种网站的灵感中,我想将导航抽屉作为一个片段来实现,包括布局和代码。但是在当时,当点击操作栏时打开导航抽屉的动作不起作用。为什么默认项目有效?这几乎是相同的代码。 代码: 主要活动: public class MyActivity extends Activity implements NavigationDrawerFragment.NavigationDrawerCallbacks { private final String TAG = getClass().getSimpleNam

从和各种网站的灵感中,我想将导航抽屉作为一个片段来实现,包括布局和代码。但是在当时,当点击操作栏时打开导航抽屉的动作不起作用。为什么默认项目有效?这几乎是相同的代码。 代码:

主要活动:

public class MyActivity extends Activity implements NavigationDrawerFragment.NavigationDrawerCallbacks {

private final String TAG = getClass().getSimpleName();

private boolean doubleBackToExitPressedOnce;

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

    NavigationDrawerFragment mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.navigation_drawer);
    mNavigationDrawerFragment.setUp(R.id.navigation_drawer, findViewById(R.id.drawer_layout));//TODO error for tablet (not DrawerLayout but Linear)
}
如果你想要所有的项目都可以在这里的页面上找到

第一次尝试:我尝试在navigationdrawerfragment类上设置此选项,但不起作用

第二次尝试:我试图在MainActivity中设置此选项,但不起作用


将此添加到NavigationDrawerFragment:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // If the drawer is open, show the global app actions in the action bar. See also
    // showGlobalContextActionBar, which controls the top-left area of the action bar.
    if (mDrawerLayout != null && isDrawerOpen()) {
        inflater.inflate(R.menu.global, menu);
        showGlobalContextActionBar();
    }
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
在这一点上,请参见onDrawerOpened末尾的设置方法:

getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()

您的代码中缺少此项,添加此项可能会起作用

是的,它们不存在,但正如您在te github链接中看到的,这不是一个活动,而是一个片段。我该怎么做?@mcmaur我想你至少需要在活动代码中加一点。你可以从片段中做很多事情,但是像onPostCreate一样应该在活动中,正如你在我编辑的答案中看到的那样,它不起作用。第二个应该是填充选项菜单,但我不需要它。事实上,如果你看一看android studio[link]中的默认应用程序,就知道没有了。@mcmaur我改变了我的答案,看看吧。这些是代码中缺少的东西。我认为这一点都不相关,因为它是右上角菜单三点的设置。事实上,我必须创建菜单xml。
@Override
        protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            // Sync the toggle state after onRestoreInstanceState has occurred.
            NavigationDrawerFragment.mDrawerToggle.syncState();
        }
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // If the drawer is open, show the global app actions in the action bar. See also
    // showGlobalContextActionBar, which controls the top-left area of the action bar.
    if (mDrawerLayout != null && isDrawerOpen()) {
        inflater.inflate(R.menu.global, menu);
        showGlobalContextActionBar();
    }
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()