【发布时间】:2014-02-22 22:52:11
【问题描述】:
我正在为包含片段的应用程序使用导航抽屉。我也在使用操作按钮。但是当我使用抽屉更改活动时,操作按钮在所有活动上。我只希望它在其中一个片段上。这是我的代码
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);
}
}
这些都是错误。
The method onCreateOptionsMenu(Menu) of type HomeFragment must override or implement a supertype method HomeFragment.java
当我在
The method getMenuInflater() is undefined for the type HomeFragment HomeFragment.java /Snatsh/src/com/colourity/snatsh
The method onCreateOptionsMenu(Menu, MenuInflater) in the type Fragment is not applicable for the arguments (Menu)
MainActivity 中执行此操作时,它工作正常,但是当我将其移至我的 HomeFragment 时,错误显示。
【问题讨论】:
-
The method onCreateOptionsMenu(Menu, MenuInflater) in the type Fragment is not applicable for the arguments (Menu)不够清楚吗?这是一个提示:您需要像错误所说的那样更改方法签名。 -
然后我会先阅读一些 Java 书籍,然后再深入 Android 开发。基本上,
Fragment和Activity有一个onCreateOptionsMenu(),但它们有不同的签名;一个需要ManuInflater,另一个只需要Menu
标签: java android xml android-fragments