为什么只调用超级实现的方法重写会使我的应用程序崩溃

Why does a method override which does nothing more than call the super implementation crashes my app

为了在一个简单的 android.support.v7.app.AppCompatActivity#AppCompatActivity 中理解 Android 生命周期,我重写了许多 "on" 方法来记录它们的执行。

我添加了以下内容:

// This override will crash app!
override fun onCreateView(name: String?, context: Context?, attrs: AttributeSet?): View {
    //log {"onCreateView(3)"}
    return super.onCreateView(name, context, attrs)
}

结果是应用程序现在崩溃了:

06-18 06:56:54.627 1004-1004/com.hanafey.prefone E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.hanafey.prefone, PID: 1004
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hanafey.prefone/com.hanafey.prefone.MainActivity}: android.view.InflateException: Binary XML file line #24: Error inflating class LinearLayout
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
        at android.app.ActivityThread.access0(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class LinearLayout
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
        at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:3605)
        at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:3677)
        at com.android.internal.policy.impl.PhoneWindow.getDecorView(PhoneWindow.java:1943)
        at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:374)
        at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:323)
        at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
        at com.hanafey.prefone.MainActivity.onCreate(MainActivity.kt:165)
        at android.app.Activity.performCreate(Activity.java:5990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
        at android.app.ActivityThread.access0(ActivityThread.java:151) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5254) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
     Caused by: java.lang.IllegalStateException: super.onCreateView(name, context, attrs) must not be null
        at com.hanafey.prefone.MainActivity.onCreateView(MainActivity.kt:59)
        at android.app.Activity.onCreateView(Activity.java:5325)
        at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:41)
        at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:68)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:482) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
        at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:3605) 
        at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:3677) 
        at com.android.internal.policy.impl.PhoneWindow.getDecorView(PhoneWindow.java:1943) 
        at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:374) 
        at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:323) 
        at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) 
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
        at com.hanafey.prefone.MainActivity.onCreate(MainActivity.kt:165) 
        at android.app.Activity.performCreate(Activity.java:5990) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
        at android.app.ActivityThread.access0(ActivityThread.java:151) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5254) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

(甚至删除了日志语句,使其成为纯非覆盖覆盖)

反身我会说这种覆盖只是一个无操作——无用但无害。

我想这一定是黑客支持旧平台的包袱中的一些反映造成的,但最好能了解真正的问题是什么。

问题是 super 可以 return null 但您的 return 类型不可为 null。如果您将其更改为以下内容,它将起作用:

override fun onCreateView(name: String?, context: Context?, attrs: AttributeSet?): View? {
    //log {"onCreateView(3)"}
    return super.onCreateView(name, context, attrs)
}