我想在动作栏中显示微调器(不在活动中,在片段中)。为了我在下面做的事情
Step-1 (spinnermenu.xml)
`<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menuSort"
android:showAsAction="ifRoom"
android:actionLayout="@layout/spinner"/>
</menu>`
Step-2 (spinner.xml)
<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="70dp"
android:layout_height="wrap_content" />
第3步(代码)
public class All extends Fragment{
ArrayList<String> spinnerlist;
ArrayAdapter<String> spinneradapter;
@Override
public void onCreate(Bundle savedInstanceState) {
setHasOptionsMenu(true);
super.onCreate(savedInstanceState);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.spinnermenu, menu);
spinnerlist = new ArrayList<String>();
spinnerlist.add("Items1");
spinnerlist.add("Items2");
spinneradapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, spinnerlist);
Spinner s = (Spinner) menu.findItem(R.id.menuSort).getActionView();
s.setAdapter(spinneradapter);
super.onCreateOptionsMenu(menu, inflater);
}
}
我在这一行收到错误
setHasOptionsMenu(true);
和此行中的空例外
s.setAdapter(spinneradapter);
有人能告诉我如何纠正这个错误吗?
* PS :有没有办法做同样的事情? *