Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 导航视图操作ABAR ONOPTIONSECTED R.id.home未触发_Android_Android Fragments_Navigation Drawer_Android Navigationview - Fatal编程技术网

Android 导航视图操作ABAR ONOPTIONSECTED R.id.home未触发

Android 导航视图操作ABAR ONOPTIONSECTED R.id.home未触发,android,android-fragments,navigation-drawer,android-navigationview,Android,Android Fragments,Navigation Drawer,Android Navigationview,我想在一个android应用程序中实现up按钮,该应用程序只有一个活动可以使用不同的片段更改其内容。 我使用了android studio提供的默认导航抽屉活动,在该活动中,我向content_main添加了一个框架布局。 在我希望显示up botton的片段中,我在onCreateView方法中添加了以下代码行: ActionBar actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar(); actionBar.s

我想在一个android应用程序中实现up按钮,该应用程序只有一个活动可以使用不同的片段更改其内容。 我使用了android studio提供的默认导航抽屉活动,在该活动中,我向content_main添加了一个框架布局。 在我希望显示up botton的片段中,我在onCreateView方法中添加了以下代码行:

ActionBar actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
在onCreate方法中添加此行:

setHasOptionsMenu(true);
我添加了捕捉点击的方法:

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        Log.w("second fragment","clicked back");
        return true;
    }
    return super.onOptionsItemSelected(item);
}
在Activity中,我按如下方式设置了CreateOptions菜单:

@Override
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;
}
但是点击它不会被触发。 我试图添加一个设置按钮,它被触发了。 我已经读了很多关于这个的问题,但我不知道如何解决它

尝试使用switch(…)case语句

 @Override
public boolean onOptionsItemSelected(MenuItem item) {

    //this will make Hamburger button clickable.
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    //this will make the HomeAsUpIndicator button clickable.
    switch (item.getItemId()) {
        case android.R.id.home:
           Log.w("second fragment","clicked back");
           break;
    }
    return super.onOptionsItemSelected(item);
}
希望这将对您有所帮助。

尝试使用switch(…)case语句

 @Override
public boolean onOptionsItemSelected(MenuItem item) {

    //this will make Hamburger button clickable.
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    //this will make the HomeAsUpIndicator button clickable.
    switch (item.getItemId()) {
        case android.R.id.home:
           Log.w("second fragment","clicked back");
           break;
    }
    return super.onOptionsItemSelected(item);
}
希望这将对您有所帮助。

sethaspoptions菜单(true)
应在方法
onCreate()中调用,以让碎片管理器知道您的碎片需要接收选项菜单回调。

sethaspoptions菜单(true)
应在方法
onCreate()中调用
让FragmentManager知道您的片段需要接收选项菜单回调。

我相信您使用的是
ActionBarDrawerToggle
,它将
工具栏作为参数。如果使用此构造函数,则如果单击该指示器,将不会调用选项ItemSelected
。有关于此问题的问题/答案,例如:

但对我来说,它们都不能完美工作,所以我使用了特定于工具栏的构造函数,这里是一个解决方案。我在工具栏顶部放置了一个透明视图,只有当我显示“后退”按钮并自己处理单击时,该视图才“可见”(在我的情况下,调用
onBackPressed()

我知道这是一种黑客行为,但它需要而且对我有效

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/main_background">

            <android.support.design.widget.AppBarLayout
                android:id="@+id/appBar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/AppTheme.AppBarOverlay">

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="?attr/colorPrimary"
                    app:popupTheme="@style/AppTheme.PopupOverlay"/>

            </android.support.design.widget.AppBarLayout>

            <FrameLayout
                android:id="@+id/layoutMainContent"
                android:layout_below="@id/appBar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

        </RelativeLayout>

        <!-- The important view -->
        <View
            android:id="@+id/viewFakeBack"
            android:layout_width="56dp"
            android:layout_height="56dp"
            android:clickable="true"
            android:visibility="gone"/>

    </FrameLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:menu="@menu/navigation_drawer"/>

</android.support.v4.widget.DrawerLayout>

我相信您使用了
ActionBarDrawerToggle
,它将
工具栏作为参数。如果使用此构造函数,则如果单击该指示器,将不会调用选项ItemSelected
。有关于此问题的问题/答案,例如:

但对我来说,它们都不能完美工作,所以我使用了特定于工具栏的构造函数,这里是一个解决方案。我在工具栏顶部放置了一个透明视图,只有当我显示“后退”按钮并自己处理单击时,该视图才“可见”(在我的情况下,调用
onBackPressed()

我知道这是一种黑客行为,但它需要而且对我有效

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/main_background">

            <android.support.design.widget.AppBarLayout
                android:id="@+id/appBar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/AppTheme.AppBarOverlay">

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="?attr/colorPrimary"
                    app:popupTheme="@style/AppTheme.PopupOverlay"/>

            </android.support.design.widget.AppBarLayout>

            <FrameLayout
                android:id="@+id/layoutMainContent"
                android:layout_below="@id/appBar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

        </RelativeLayout>

        <!-- The important view -->
        <View
            android:id="@+id/viewFakeBack"
            android:layout_width="56dp"
            android:layout_height="56dp"
            android:clickable="true"
            android:visibility="gone"/>

    </FrameLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:menu="@menu/navigation_drawer"/>

</android.support.v4.widget.DrawerLayout>


我把它放进去了,但我忘了在问题中加上如何在CreateOptions菜单上实现
一切似乎都正常:你调用了SethasOptions菜单,你使用android.R.id.home而不是R.id.home,只要CreateOptions菜单正常。我不知道。orz…已经尝试了and和nor两种方法。奇怪的是,即使在
R.id.home
上的活动
onoptions itemselected
中也没有触发。我把它放在这里,但我忘了在问题中添加如何实现
onCreateOptions菜单
?所有事情似乎都正常:您调用了SetHasOptions菜单,只要CreateOptions菜单正常,您就可以使用android.R.id.home而不是R.id.home。我不知道。orz….已经尝试了and和nor两种方法。奇怪的是,即使在活动中,
onOptionsItemSelected
on
R.id.home
也没有触发Hank的,最后我从各种问题中找到了另一种解决方法,因为对于我和workThank来说,最后我从各种问题中找到了另一种解决方法,因为对于我和WorkCorry来说,在我的情况下,不会调用整个OnOptions ItemSelected。这没有任何意义。为什么开关与此问题有关?对不起,在我的情况下,不会调用整个OnOptions ItemSelected。这没有任何意义。为什么开关与问题有关?