如何从其他类调用onCreate方法?
我想调用 onCreate(Bundle cicici);从其他类,然后我得到“NullPointerException”,所以请指导我如何从另一个类调用 onCreate() 。
I want to call onCreate(Bundle cicici); from other class then i am getting "NullPointerException", so please guide me how can i call the onCreate() from another class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只有一种方法可以调用 onCreate,即启动 Activity,因为 onCreate 是 Activity 生命周期的一部分。
There is only one way in which onCreate can be called, by starting an Activity, since onCreate is as part of Activity life cycle.
如果你想调用onCreate来实际呈现一个新屏幕,那么你需要使用android框架风格创建一个新的Activity。
成分:
1- 一个调用新活动的事件(即触发按钮或列表的 onClickListener)
2-在事件中,您需要使用当前活动的引用和 class 新活动的引用,例如:
3- 您需要根据需要使用 startActivity 或 startActivityForResult 来调用此活动,最后一个是在您期望活动得到响应时使用。
您还可以参考Android文档常见任务,如果有帮助请告诉我们
if you want to call onCreate in order to actually present a new screen, then you need to create a the new Activity using the android framework style.
Ingredients:
1- An event to call your new activity( ie. onClickListener of a Button or list triggered)
2- On the event you need to create an Intent with the reference of the current activity and a class reference of your new Activity, example:
3- You need to call this activity depending on what you'll need you use startActivity or startActivityForResult, the last is use when you expect a response from your activity.
You can also refer to Android documentation Common Task, let us know if its helpful
这取决于您想在第二个活动中做什么。如果您想创建一个简单的任务,您始终可以使用对话框,并且可以在您的活动中显示它们。
或者,再想一想,您可以隐藏一些观点并启用其他观点,但我想这不是一个正统的解决方案:)
It depends what you want to do in the second activity. If you want to create a simple task you can always use dialogs and you can show them inside your activity.
Or, on a second thought, you can hide some of your views and enable others but I guess that's not an orthodox solution :)