我正在为包含片段的应用程序使用导航抽屉。我也在使用动作图标。但是,当我使用抽屉更改活动时,所有活动上都带有操作图标。我只想要其中一个片段。这是我的代码

package com.colourity.snatsh;

import com.colourity.snatsh.R;
import java.util.ArrayList;
import com.colourity.snatsh.adapter.NavDrawerListAdapter;
import com.colourity.snatsh.model.NavDrawerItem;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;


public class HomeFragment extends Fragment {

    public HomeFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

        return rootView;
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }
}


当我在HomeFragment中尝试时,它似乎不起作用。 Here是我正在使用的指南。

编辑:

尝试在HomeFragment中的指南时出现以下错误

The method onCreateOptionsMenu(Menu, MenuInflater) in the type Fragment is not applicable for the arguments (Menu) HomeFragment.java /Snatsh/src/com/colourity/snatsh line 29 Java Problem

The method getMenuInflater() is undefined for the type HomeFragment HomeFragment.java /Snatsh/src/com/colourity/snatsh line 27 Java Problem

The method onCreateOptionsMenu(Menu) of type HomeFragment must override or implement a supertype method HomeFragment.java /Snatsh/src/com/colourity/snatsh line 25 Java Problem

最佳答案

您的onCreateOptionsMenu缺少参数,请尝试以下操作:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // Inflate the menu items for use in the action bar
    inflater.inflate(R.menu.main, menu);
}


并添加其他导入(如果未自动完成):

import android.view.MenuInflater;

10-07 17:00