如何扩展 SomeActivity 来重新定位例如 onCreateOptionsMenu?

发布于 2024-09-03 13:16:32 字数 773 浏览 7 评论 0原文

我知道这应该相当简单,但我无论如何都不明白。
我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可是我不能没有你 2024-09-10 13:16:32

你在这里误解了继承。
如果您想在子类中填充菜单,请尝试以下操作:

public class XyActivity extends XyOptions {
    public void onCreate(Bundle savedInstanceState) {
       //.. other 
    }
}
public class XyOptions extends Activity {

    public EditImageOptions() {...}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {...}

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {...}

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {...}
}

You are misunderstanding the inheritance here.
If you want to populate the menu in a subclass, try this:

public class XyActivity extends XyOptions {
    public void onCreate(Bundle savedInstanceState) {
       //.. other 
    }
}
public class XyOptions extends Activity {

    public EditImageOptions() {...}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {...}

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {...}

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {...}
}
风启觞 2024-09-10 13:16:32

我认为您想要做的是,通过实例化 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文