如何扩展 SomeActivity 来重新定位例如 onCreateOptionsMenu?
我知道这应该相当简单,但我无论如何都不明白。
我有一个 Activity(我们称之为XyActivity),它已经变得相当长了。因此,我想将一些重写的方法重新定位到子类(我们称之为XyOptions)。看起来像这样:
public class XyActivity extends Activity {
XyOptions xyOptions;
public void onCreate(Bundle savedInstanceState) {
xyOptions = new XyOptions();
...
}
}
不幸
public class XyOptions extends XyActivity {
public EditImageOptions() {...}
@Override
public boolean onCreateOptionsMenu(Menu menu) {...}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {...}
@Override
public boolean onOptionsItemSelected(MenuItem item) {...}
的是,XyOptions 中的方法从未被调用。我不明白为什么。我确信答案是微不足道的,所以请指出我的方向。
谢谢,
斯特夫
I know this should be fairly simple but I don't get it anyhow.
I've got an Activity (let's call it XyActivity) which has gotten pretty long. Therefore, I'd like to relocate some overriden methods to a subclass (let's call it XyOptions). Looks like that:
public class XyActivity extends Activity {
XyOptions xyOptions;
public void onCreate(Bundle savedInstanceState) {
xyOptions = new XyOptions();
...
}
}
and
public class XyOptions extends XyActivity {
public EditImageOptions() {...}
@Override
public boolean onCreateOptionsMenu(Menu menu) {...}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {...}
@Override
public boolean onOptionsItemSelected(MenuItem item) {...}
Unfortunately, the methods in XyOptions are never called. And I don't get why. I'am sure the answer is trivial so please point me in some direction.
Thanks,
Steff
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你在这里误解了继承。
如果您想在子类中填充菜单,请尝试以下操作:
You are misunderstanding the inheritance here.
If you want to populate the menu in a subclass, try this:
我认为您想要做的是,通过实例化 XyActivity 中的 XyOptions 对象,在活动 XyActivity 中调用 XyOptions 中的 onCreateOptionsMenu。据我所知这是不可能的。 onCreateOptionsMenu 仅在按下手机上的菜单按钮时调用。
正如已经指出的,您应该纠正继承流程。
I think what you are trying to do is, invoke onCreateOptionsMenu in XyOptions while being in the activity XyActivity, by instantiating an object of XyOptions in XyActivity. Its not possible as far I know. onCreateOptionsMenu is only invoked on pressing menu button on the phone.
You should correct the flow of inheritance, as pointed out already.