3

我从 MainActivity 扩展了一个“登录”类。

MainActivity 看起来像这样:

 public class MainActivity extends FragmentActivity {
/** Called when the activity is first created. */
private static String TAG = "MainActivity";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.main);

    Intent login_activity = new Intent(this, Login.class);
    startActivity(login_activity);
    Log.d(TAG,"Login created");
}
}

在 login.class 我只是调用布局:

public class Login extends MainActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    //super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
     }
}

你可以看到,我在登录类中注释掉了“super.oncreate”。在这里再次调用 MainActivity oncreate 是没有意义的。但是这个被淘汰的 super.oncreate 我会得到一些例外:

12-31 11:37:47.688: E/AndroidRuntime(4206): FATAL EXCEPTION: main
12-31 11:37:47.688: E/AndroidRuntime(4206): android.app.SuperNotCalledException: Activity {de.svennergr.htn/de.svennergr.htn.Login} did not call through to super.onCreate()
12-31 11:37:47.688: E/AndroidRuntime(4206):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2146)
12-31 11:37:47.688: E/AndroidRuntime(4206):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-31 11:37:47.688: E/AndroidRuntime(4206):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-31 11:37:47.688: E/AndroidRuntime(4206):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-31 11:37:47.688: E/AndroidRuntime(4206):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-31 11:37:47.688: E/AndroidRuntime(4206):     at android.os.Looper.loop(Looper.java:137)
12-31 11:37:47.688: E/AndroidRuntime(4206):     at android.app.ActivityThread.main(ActivityThread.java:5039)
12-31 11:37:47.688: E/AndroidRuntime(4206):     at java.lang.reflect.Method.invokeNative(Native Method)
12-31 11:37:47.688: E/AndroidRuntime(4206):     at java.lang.reflect.Method.invoke(Method.java:511)
12-31 11:37:47.688: E/AndroidRuntime(4206):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-31 11:37:47.688: E/AndroidRuntime(4206):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-31 11:37:47.688: E/AndroidRuntime(4206):     at dalvik.system.NativeStart.main(Native Method)

当我不评论 super.oncreate 时,我会得到一个不停止的循环,创建许多“登录”对象/活动。

我该如何解决这个问题?

4

4 回答 4

4

In Android, you must adhere to the life cycle of the Activities. Each time you override a life cycle method, you must call super to ensure consistency with Activities life cycle.

The point here is not to call MainActivity.onCreate but Activity.onCreate. If you don't want to inherit from the behavior of MainActivity inside LoginActivity, then don't extend from it. Maybe you need an intermediate parent common parent class to group only the desired behavior.

于 2012-12-31T10:52:30.057 回答
2

You must always explicitly call onCreate() superclass method when subclassing Activity so the system could properly initialize it so you can't just eliminate the call to superclass onCreate().

It's generally not a very good idea to start an activity in another activity's onCreate() method. Just think about other ways of launching your Login activity (make a button that launches it e.g.).

If you are planning to have multiple subclasses of your MainActivity just incapsulate the common behavior and components in it and launch your subclasses (Login activity) directly. Or if you want the Login activity appear in the beginning you can make it a LAUNCHER activity in AndroidManifest.xml.

Please, read more about activity lifecycles and study some theory before development.

于 2012-12-31T10:54:05.360 回答
0

规则是,如果您覆盖 onCreate(),则必须调用 super.onCreate()...您可以做的是在 MainActivity 上设置 setContentView(),如下所示: setContentView(getCurrentViewLayoutId()) 和 getCurrentViewLayoutId() 将是LoginActivity.java 将实现的方法......像这样,您可以从 LoginActivity.java 中完全删除 onCreate() 实现......然后“super.onCreate() 检查”不会有任何问题。但在 onResume() 中调用 startActivity 可能是个好主意。

于 2012-12-31T10:55:17.067 回答
0

是否有任何扩展 MainActivity 以进行登录的具体原因?

由于您在 MainActivity 中调用 Login 意图并且 Login 需要调用 super.onCreate() 它将进入无限循环。所以,我认为最好扩展 Activity 而不是 MainActivity。

于 2012-12-31T10:56:39.383 回答