我想将 ActionBar 添加到我的 Fragment 类中,但我不断收到错误。这是我的课:
public class TaskDetailFragment extends Fragment {
public static final String ARG_ITEM_ID = "item_id";
MyTasks.taskItem mItem;
public TaskDetailFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments().containsKey(ARG_ITEM_ID)) {
mItem = MyTasks.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_task_detail,
container, false);
ActionBar actionBar = getActionBar();
actionBar.show();
if (mItem != null) {
//Add TexViews
((TextView) rootView.findViewById(R.id.in_Name))
.setText(MyTasks.customers[(Integer.parseInt(mItem.id))][1]);
((TextView) rootView.findViewById(R.id.in_Address))
.setText(MyTasks.customers[(Integer.parseInt(mItem.id))][3] + " " + MyTasks.customers[(Integer.parseInt(mItem.id))][2]);
}
return rootView;
}
}
这是我的错误:
The method getActionBar() is undefined for the type TaskDetailFragment
最佳答案
据我所知,您需要向 Activity
(已附加 Fragment
)询问 ActionBar
:
ActionBar actionBar = getActivity().getActionBar();
或者,如果您正在使用支持库和/或 ActionBarSherlock:
ActionBar actionBar = getFragmentActivity().getSupportActionBar();
fragment 未定义 getActionBar()
方法,因此出现错误消息。
关于java - getActionBar() 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13109708/