gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:00:13 25 4
gpt4 key购买 nike

为了通过简单的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.access$800(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.access$800(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.access$800(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) 

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

本能地,我会说这种覆盖只是一个无操作——无用但无害。

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

最佳答案

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

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

关于android - 为什么只调用 super 实现的方法重写会使我的应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50911533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com