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批注时,菜单项不可见_Android_Menuitem_Android Annotations - Fatal编程技术网

使用android批注时,菜单项不可见

使用android批注时,菜单项不可见,android,menuitem,android-annotations,Android,Menuitem,Android Annotations,当我运行代码时,菜单项不可见。我想我遗漏了什么。 下面是我的代码细节。我已经参考了android注释文档。谢谢 格雷德尔先生 def AAVersion = '4.3.1' dependencies { annotationProcessor "org.androidannotations:androidannotations:$AAVersion" compile "org.androidannotations:androidannotations-api:$AAVersion

当我运行代码时,菜单项不可见。我想我遗漏了什么。 下面是我的代码细节。我已经参考了android注释文档。谢谢

格雷德尔先生

def AAVersion = '4.3.1'
dependencies {
    annotationProcessor "org.androidannotations:androidannotations:$AAVersion"
    compile "org.androidannotations:androidannotations-api:$AAVersion"
}
MainActivity.java

@EActivity(R.layout.activity_main)
@OptionsMenu(R.menu.activity_menu)
public class MainActivity extends BaseActivity {

        @OptionsMenuItem(R.id.menuShare)
        MenuItem menuShare;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            ProfileListFragment fragment = ProfileListFragment.newInstance();
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.container, fragment)
                    .commit();
        }

        @OptionsItem(R.id.menuShare)
        void menuShareClick() {
            Toast.makeText(this, "Menu Share", Toast.LENGTH_SHORT).show();
        }
    }
activity_menu.xml

<?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/menuShare"
            android:title="@string/share" />
    </menu> 
fragment_profile_list.xml

<RelativeLayout 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"
    tools:context="com.example.android.biodata.activities.fragments.ProfileListFragment">

    <include
        android:id="@+id/toolbar"
        layout="@layout/layout_toolbar" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/btnFAB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_marginBottom="10dp"
        android:layout_marginEnd="10dp"
        android:src="@android:drawable/ic_input_add"
        android:tint="@color/white"
        app:backgroundTint="@color/colorAccent" />

</RelativeLayout>

我应该将菜单项和工具栏放在片段中吗? 我也无法在fragment中设置工具栏。(这里面临另一个问题)
请更正我的理解。

您在
片段中缺少以下行:

@AfterViews
void afterViews() {
    ((BaseActivity) getActivity()).setSupportActionBar(toolbar);
}
也不要忘记在onDestroyView中删除它:

@Override
void onDestroyView() {
    ((BaseActivity) getActivity()).setSupportActionBar(null);
    super.onDestroyView();
}

您还必须设置工具栏(或操作栏)。@WonderCsabo我已经更新了我的问题。我有一个包含ProfileListFragment的MainActivity。我正在尝试在我的应用程序的每个片段中创建新的工具栏。
@Override
void onDestroyView() {
    ((BaseActivity) getActivity()).setSupportActionBar(null);
    super.onDestroyView();
}