super.onCreate(savedInstanceState); [英] super.onCreate(savedInstanceState);

查看:297
本文介绍了super.onCreate(savedInstanceState);的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个新手,我有很基本的问题。我创建了Eclipse的一个Android应用程序项目。在MainActivity.java - 的onCreate()正在呼叫

Being a newbie, I have very basic question. I created an Android Application Project in Eclipse. In MainActivity.java - onCreate() it is calling

super.onCreate(savedInstanceState)

谁能解释什么是以上行的目的是什么?

Can anyone explain what is the purpose of above line?

推荐答案

你做的每一次活动是通过方法调用的顺序启动。 的onCreate()是第一个这些调用。

Every Activity you make is started through a sequence of method calls. onCreate() is the first of these calls.

你的活动每一位扩展 android.app.Activity 直接或通过继承活动的另一个小类

Each and every one of your Activities extends android.app.Activity either directly or by subclassing another subclass of Activity.

在Java中,当你从一个类继承,可以覆盖其方法,以在其中运行自己的code。这方面的一个非常常见的例子是的toString压倒一切的()扩展方法时,的java.lang.Object

In Java, when you inherit from a class, you can override its methods to run your own code in them. A very common example of this is the overriding of the toString() method when extending java.lang.Object.

在我们覆盖的方法,我们完全替代我们班的方法,或者扩展现有的父类的方法的选择。通过调用 super.onCreate(savedInstanceState); ,你告诉Dalvik虚拟机来除现有的$ C $运行code c在父类的OnCreate()。如果你离开了这一行,然后点击只是你的code 运行。现有的code完全被忽略。

When we override a method, we have the option of completely replacing the method in our class, or of extending the existing parent class' method. By calling super.onCreate(savedInstanceState);, you tell the Dalvik VM to run your code in addition to the existing code in the onCreate() of the parent class. If you leave out this line, then only your code is run. The existing code is ignored completely.

不过,你必须在你的方法这个超级调用,因为如果你不那么的onCreate() code。在活动永远不会运行,并且你的应用程序会遇到各种各样的问题,就像没有分配给活动(背景下,虽然你会打一个 SuperNotCalledException 之前,你有机会搞清楚,你有没有上下文)。

However, you must include this super call in your method, because if you don't then the onCreate() code in Activity is never run, and your app will run into all sorts of problem like having no Context assigned to the Activity (though you'll hit a SuperNotCalledException before you have a chance to figure out that you have no context).

总之,Android的自身的类可以是极其复杂的。在code在框架类处理的东西,如UI绘制,内部清理和维护活动和应用程序的生命周期。 呼叫使开发人员能够运行在幕后这一复杂code,同时还提供了一个抽象良好的水平,为自己的应用程序。

In short, Android's own classes can be incredibly complex. The code in the framework classes handles stuff like UI drawing, house cleaning and maintaining the Activity and application lifecycles. super calls allow developers to run this complex code behind the scenes, while still providing a good level of abstraction for our own apps.

这篇关于super.onCreate(savedInstanceState);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆