gpt4 book ai didi

android - onCreateOptionsMenu 在 onCreate android 之前调用

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:19:46 27 4
gpt4 key购买 nike

我是 android 的初学者,我正在开发小型 android 应用程序。在我的应用程序中,我使用的是 Sherlock 库。我的应用程序包含一个主要 Activity 和两个 fragment 。在我的列表 fragment 结构中看起来像

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");

}

}

现在我的问题是,当我第一次从一个 fragment 切换到另一个 fragment 时它给出以下日志输出

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 和 on activity create 之前调用 on create 选项。它也给出了两个警告。这项工作没有任何错误。但是我无法显示我的 ListView

现在,当我再次切换到同一个 fragment (第二次)时,它会给出以下日志输出

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

这次它没有给出任何警告并且工作正常。还显示我的 ListView 。但是第一次它不能正常工作..另一点是,当我在更高版本的 android 上运行它时,它工作正常。但是如果我在像 2.3.3 这样的较低版本的 android 上运行它,我会显示这样的行为..我读到了这个 ListFragment onPrepareOptionsMenu called before onCreate. Why and How to Fix / Bypass?但是我无法解决这个问题。

如果我在 onActivityCreated 中定义 setHasOptionsMenu(true) 它会给出以下输出

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

我还尝试为我的 fragment 设置 setHasOptionsMenu,其中实际的 fragment 事务发生如下所示......

@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();
}

它仍然给出同样的问题。

我的代码或其他问题有什么问题吗?那么如何解决这个问题。需要帮忙...谢谢...

最佳答案

onActivityCreated(Bundle savedInstanceState)方法中写入setHasOptionsMenu(true);

关于android - onCreateOptionsMenu 在 onCreate android 之前调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14744537/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com