android:自己的类扩展活动和/或列表活动
我在整个应用程序的所有活动中都有一些相同的东西,例如选项菜单和一些需要运行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您可以在
Activity
子类(例如MyActivity
)中实现所有功能,并使MyListActivity
类成为的子类MyActivity
类。另一种方法是创建一个辅助类,其中包含静态方法中的所有功能,并以 Activity 对象作为第一个参数。在这种情况下,您不需要创建
MyActivity
或MyListActivity
类,但需要在每个Activity
子类中调用辅助类的方法你想继承这些功能。I think you can implement all the features in an
Activity
subclass (e.g.,MyActivity
) and make theMyListActivity
class a subclass of theMyActivity
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 createMyActivity
orMyListActivity
classes, but you need to call methods of the helper class in everyActivity
subclass you want to inherit these features.