Android onCreateOptionMenu 没有被调用
我正在实现一个 Android Activity,其他 Activity 将从该 Activity 派生。所以基本上我有一个 InventoryActivity 及其父类 ListActivity 的设置:
public class MyListActivity extends Activity {
protected Context mContext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this.getBaseContext();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options, menu);
Log.d("Creating options menu", "True");
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
Log.d("Preparing options menu", "True");
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.save:
return(true);
case R.id.revert:
return(true);
}
return(super.onOptionsItemSelected(item));
}
}
public class InventoryActivity extends MyListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inventory);
}
}
并且我也在 options.xml 中设置了这个设置
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/card_list_save"
android:icon="@drawable/ic_menu_save"
android:title="Save"/>
<item android:id="@+id/card_list_revert"
android:icon="@drawable/ic_menu_revert"
android:title="Revert" />
</menu>
。如果有必要,这是我的 inventory.xml 布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/callSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Search"/>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/inventory"/>
</ScrollView>
</LinearLayout>
但是,当我按下菜单按钮时,没有任何反应。 onCreateOptionsMenu 方法中的日志消息不会出现。相反,我能看到的只是以下内容:
02-04 11:36:58.313: W/KeyCharacterMap(31464): No keyboard for id 0
02-04 11:36:58.313: W/KeyCharacterMap(31464): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
但最让我困惑的是这段代码可以在其他活动中工作,例如我的启动器活动。但根据面向对象编程的概念,InventoryActivity 应该调用 MyListActivity 中的重写方法。我完全陷入困境,我需要帮助。
I am implementing an Android Activity from which other Activities will be derived from. So basically I have this setup of an InventoryActivity and its parent class, ListActivity:
public class MyListActivity extends Activity {
protected Context mContext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this.getBaseContext();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options, menu);
Log.d("Creating options menu", "True");
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
Log.d("Preparing options menu", "True");
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.save:
return(true);
case R.id.revert:
return(true);
}
return(super.onOptionsItemSelected(item));
}
}
public class InventoryActivity extends MyListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inventory);
}
}
And I also have this in options.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/card_list_save"
android:icon="@drawable/ic_menu_save"
android:title="Save"/>
<item android:id="@+id/card_list_revert"
android:icon="@drawable/ic_menu_revert"
android:title="Revert" />
</menu>
If necessary, this is my layout for inventory.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/callSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Search"/>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/inventory"/>
</ScrollView>
</LinearLayout>
However, when I press the menu button, nothing happens. The Log messages in the onCreateOptionsMenu method does not appear. Instead all I can see is the following:
02-04 11:36:58.313: W/KeyCharacterMap(31464): No keyboard for id 0
02-04 11:36:58.313: W/KeyCharacterMap(31464): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
But what baffles me the most is that this code works in other Activities, such as my launcher Activity. But by the concept of object oriented programming, the InventoryActivity should call the overriding methods in the MyListActivity. I am completely stuck and I need help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ListActivity
已经是 Android SDK 中的一个类。我的猜测是您正在导入android.app.ListActivity
,而不是您的包。ListActivity
is already a class in the Android SDK. My guess is that you're importingandroid.app.ListActivity
, and not your package.嗯...不知道为什么,但删除
MyListActivity
中的onCreate
方法解决了问题。所以类现在看起来像这样:Hmm...don't know why but removing the
onCreate
method inMyListActivity
fixed the problem. So the class now looks like this: