我是 android 的初学者,我正在开发小型 android 应用程序。在我的应用程序中,我使用的是 Sherlock 库。我的应用程序包含一个主要活动和两个片段。我的列表片段结构看起来像
public class MyCards extends SherlockListFragment
{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
Log.i("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "inside fragment on create");
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "inside on activity created");
}
private void displayCards(int type) {
Log.i("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "inside display cards");
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.mycards, container, false);
View view1 = inflater.inflate(R.layout.empty_card, container, false);
return view;
}
/*@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
optionsMenu = menu;
}*/
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_type2, menu);
Log.i("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "inside on create option menu");
Spinner spinner = (Spinner) menu.findItem(R.id.cardType)
.getActionView();
SpinnerAdapter adapter = ArrayAdapter.createFromResource(getSherlockActivity().getSupportActionBar().getThemedContext(), R.array.card_action_list,android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter); // set the adapter
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
displayCards(pos);
Log.i("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "inside on option selected");
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
displayCards(0);
Log.i("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "inside on no option selection");
}
});
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.i("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "inside option select listener");
}
}
现在我的问题是,当我第一次从一个片段切换到另一个片段时,它会给出以下日志输出
01-04 00:54:06.997: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(2575): inside on create option menu
01-04 00:54:06.997: W/KeyCharacterMap(2575): No keyboard for id -1
01-04 00:54:06.997: W/KeyCharacterMap(2575): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
01-04 00:54:07.020: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(2575): inside fragment on create
01-04 00:54:07.036: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(2575): inside on activity created
这意味着在 onfragment create 和活动 create 之前调用的 create 选项。它还给出了两个警告。这项工作没有任何错误。但我无法显示我的列表视图
现在,当我再次切换到同一个片段(第二次)时,它会给出以下日志输出
02-07 10:05:10.983: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(15844): inside on create option menu
02-07 10:05:11.023: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(15844): inside fragment on create
02-07 10:05:11.043: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(15844): inside on activity created
02-07 10:05:11.113: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(15844): inside display cards
02-07 10:05:11.133: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(15844): inside on option selected
这次它没有发出任何警告并且工作正常。还显示我的列表视图。但是第一次它不能正常工作。另一点是,当我在更高版本的 android 上运行它时,它工作正常。但是,如果我在 2.3.3 等较低版本的 android 上运行它,我会显示这种行为。我读到了在 onCreate 之前调用的这个 ListFragment onPrepareOptionsMenu。为什么以及如何修复/绕过? 但我无法解决这个问题。
如果我setHasOptionsMenu(true)
在里面定义onActivityCreated
它会给出以下输出
01-04 03:32:38.622: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(8157): inside fragment on create
01-04 03:32:38.653: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(8157): inside on activity created
01-04 03:32:38.692: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(8157): inside on create option menu
01-04 03:32:38.700: W/KeyCharacterMap(8157): No keyboard for id -1
01-04 03:32:38.700: W/KeyCharacterMap(8157): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
我还尝试为我的片段设置 setHasOptionsMenu,其中实际片段事务发生如下..
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
MyCards mc = new MyCards();
mc.setHasOptionsMenu(true);
ft = mActivity.getSupportFragmentManager().beginTransaction();
mFragment = Fragment.instantiate(mActivity, mClass.getName(),
mArgs);
ft.add(android.R.id.content, mFragment, mTag);
ft.commit();
}
仍然存在同样的问题。
我的代码或其他问题有什么问题吗?那么如何解决这个问题。需要帮助...谢谢...