android:自己的类扩展活动和/或列表活动

发布于 2024-10-29 23:51:42 字数 513 浏览 4 评论 0原文

我在整个应用程序的所有活动中都有一些相同的东西,例如选项菜单和一些需要运行 onresume、onrestart 和 onpause 的代码。我认为将它们放入我的类 MyListActivity 扩展 ListActivity 中,然后让我的所有活动扩展 MyListActivity 是一种明智的方法。

这一切都很好,直到我创建了一个没有 ListView 的活动。应用程序崩溃是因为 ListActivity 需要 ListView。但是,这个新活动不需要 ListView,但仍然需要 MyListActivity 中的所有功能/覆盖。

现在我能想到两个解决方案。一:向布局添加一个虚拟列表视图,其visibility = false,height & width = 0(还没有尝试过,但我想它应该可以工作)。第二:将 MyListActivity 类的内容复制/粘贴到 MyActivity 扩展 Activity 类中。我觉得这样做很愚蠢,但我对如何解决这个问题没有任何其他想法。

关于如何更好地处理这个问题有什么想法吗?

谢谢

I have a couple of things which are the same in all my Activities throughout my application, e.g. an optionsmenu and some code which needs to run onresume, onrestart and onpause. I figured it would be a smart approach to put them in my a class MyListActivity extends ListActivity and then have all my activities extending MyListActivity.

This worked out just fine until I created an activity which didn't have a ListView. the App crashes because ListActivity expects a ListView. However, this new activity does not need a ListView, but would still need all my functions / Overrides in MyListActivity .

Right now I can think of two solutions. One: add a dummy listview to the layout with visibility = false, height & width = 0 (haven't tried this, but i guess it should work). And Two: copy/paste the contents of the MyListActivity class into a MyActivity extends Activity class. I feel very silly doing this, but I don't have any other ideas on how to solve this issue.

Any ideas on how to handle this nicer?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

走过海棠暮 2024-11-05 23:51:42

我认为您可以在 Activity 子类(例如 MyActivity)中实现所有功能,并使 MyListActivity 类成为 的子类MyActivity 类。

另一种方法是创建一个辅助类,其中包含静态方法中的所有功能,并以 Activity 对象作为第一个参数。在这种情况下,您不需要创建 MyActivityMyListActivity 类,但需要在每个 Activity 子类中调用辅助类的方法你想继承这些功能。

I think you can implement all the features in an Activity subclass (e.g., MyActivity) and make the MyListActivity class a subclass of the MyActivity class.

Other approach is to make a helper class which contains all the features in static methods with an Activity object as the first argument. In this case you don't need to create MyActivity or MyListActivity classes, but you need to call methods of the helper class in every Activity subclass you want to inherit these features.

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