我正在使用包含片段的應用程序的導航抽屜。我也在使用操作按鈕。但是當我使用抽屜改變活動時,動作按鈕就在所有活動上。我只想要在其中一個片段上。這是我的代碼1活動的動作按鈕?
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
錯誤顯示。
'Fragment類型的方法onCreateOptionsMenu(Menu,MenuInflater)不適用於參數(Menu)'不夠清楚嗎?這裏有個提示:你需要像錯誤說的那樣改變方法簽名。 –
然後,我將從閱讀一些Java書開始,然後深入Android開發。基本上,'Fragment'和'Activity'有一個'onCreateOptionsMenu()',但它們有不同的簽名;一個需要一個'ManuInflater'另一個只需要'Menu' – Emmanuel