【错误记录】布局组件加载错误 ( Attempt to invoke virtual method ‘xxx$Callback android.view.Window.getCallback()‘ )

2023-03-29 09:06:15 浏览数 (1)

文章目录

  • 一、报错信息
  • 二、解决方案

一、报错信息


使用环境 :

  • Kotlin 编写的 Activity
  • 使用了 kotlin-android-extensions 扩展插件 , 在 build.gradle 中配置的 apply plugin: 'kotlin-android-extensions' ;
  • 在 Activity 界面中使用了 var texts: Array<TextView> = arrayOf(text0, text1) 代码 ;
代码语言:javascript复制
2021-03-26 11:30:49.653 8620-8620/cn.hsl.example E/AndroidRuntime: FATAL EXCEPTION: main
    Process: cn.hsl.example, PID: 8620
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{cn.hsl.example/cn.hsl.example.xxxActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2881)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3086)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6718)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
        at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:120)
        at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:155)
        at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:31)
        at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:55)
        at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:33)
        at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:33)
        at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201)
        at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
        at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
        at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
        at cn.hsl.example.xxxActivity._$_findCachedViewById(Unknown Source:25)
        at cn.hsl.example.xxxActivity.<init>(ReactionDrumActivity.kt:47)
        at java.lang.Class.newInstance(Native Method)
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1215)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2869)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3086) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6718) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
2021-03-26 11:30:49.654 8620-8620/cn.hsl.example E/DEBUG: begin to generate java report
2021-03-26 11:30:49.659 8620-8620/cn.hsl.example E/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
2021-03-26 11:30:49.661 8620-8620/cn.hsl.example E/DEBUG: Basic Information: 'pid: 8620/tid: 8620/time: 20210326113049'
2021-03-26 11:30:49.662 8620-8620/cn.hsl.example E/DEBUG: Cpu Information: 'abi: arm64-v8a,armeabi-v7a,armeabi/processor: AArch64 Processor rev 1 (aarch64)/hardware: Qualcomm Technologies, Inc MSM8998'
2021-03-26 11:30:49.662 8620-8620/cn.hsl.example E/DEBUG: Mobile Information: 'model: Pixel 2/version: 9/sdk: 28'
2021-03-26 11:30:49.662 8620-8620/cn.hsl.example E/DEBUG: Build fingerprint: 'google/walleye/walleye:9/PQ3A.190801.002/5670241:user/release-keys'
2021-03-26 11:30:49.664 8620-8620/cn.hsl.example E/DEBUG: Runtime Information: 'start: 20210326113042/maxheap: 201326592/primaryabi: arm64-v8a/ground: bg'
2021-03-26 11:30:49.665 8620-8620/cn.hsl.example E/DEBUG: Application Information: 'version: 0.1/subversion: release/buildseq: d79b3587/versioncode: 1'
2021-03-26 11:30:49.665 8620-8620/cn.hsl.example E/DEBUG: CrashSDK Information: 'version: 3.2.0.4/nativeseq: 0/javaseq: 200403192109/arch: /target: umeng'
2021-03-26 11:30:49.666 8620-8620/cn.hsl.example E/DEBUG: Report Name: 5f3cf3dfd3093221547aec89_0.1_d79b3587_Pixel-2_9_161672944965535295_20210326113049_bg_ucebujava.log
2021-03-26 11:30:49.666 8620-8620/cn.hsl.example E/DEBUG: UUID: 1affcaf9-c824-4c86-acfe-80132e09c1fb
2021-03-26 11:30:49.667 8620-8620/cn.hsl.example E/DEBUG: Log Type: ucebujava
2021-03-26 11:30:49.667 8620-8620/cn.hsl.example E/DEBUG: Activity: (none)
2021-03-26 11:30:49.667 8620-8620/cn.hsl.example E/DEBUG: --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
2021-03-26 11:30:49.667 8620-8620/cn.hsl.example E/DEBUG: um_app_key: 5f3cf3dfd3093221547aec89
2021-03-26 11:30:49.667 8620-8620/cn.hsl.example E/DEBUG: um_app_channel: Umeng
2021-03-26 11:30:49.667 8620-8620/cn.hsl.example E/DEBUG: um_os: android
2021-03-26 11:30:49.668 8620-8620/cn.hsl.example E/DEBUG: um_crash_sdk_version: v0.0.5
2021-03-26 11:30:49.668 8620-8620/cn.hsl.example E/DEBUG: um_umid_header: ai3406fbd3ecf72697d53b52606d7202ee
2021-03-26 11:30:49.668 8620-8620/cn.hsl.example E/DEBUG: --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
2021-03-26 11:30:49.668 8620-8620/cn.hsl.example E/DEBUG: Process Name: 'cn.hsl.example'
2021-03-26 11:30:49.668 8620-8620/cn.hsl.example E/DEBUG: Thread Name: 'main'
2021-03-26 11:30:49.668 8620-8620/cn.hsl.example E/DEBUG: Back traces starts.
2021-03-26 11:30:49.668 8620-8620/cn.hsl.example W/cn.hsl.example: Accessing hidden field Ljava/lang/Throwable;->detailMessage:Ljava/lang/String; (light greylist, reflection)
2021-03-26 11:30:49.669 8620-8620/cn.hsl.example E/DEBUG: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{cn.hsl.example/cn.hsl.example.xxxActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
2021-03-26 11:30:49.670 8620-8620/cn.hsl.example E/DEBUG:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2881)
2021-03-26 11:30:49.670 8620-8620/cn.hsl.example E/DEBUG:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3086)
2021-03-26 11:30:49.670 8620-8620/cn.hsl.example E/DEBUG:     at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
2021-03-26 11:30:49.670 8620-8620/cn.hsl.example E/DEBUG:     at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
2021-03-26 11:30:49.670 8620-8620/cn.hsl.example E/DEBUG:     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
2021-03-26 11:30:49.671 8620-8620/cn.hsl.example E/DEBUG:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
2021-03-26 11:30:49.671 8620-8620/cn.hsl.example E/DEBUG:     at android.os.Handler.dispatchMessage(Handler.java:106)
2021-03-26 11:30:49.671 8620-8620/cn.hsl.example E/DEBUG:     at android.os.Looper.loop(Looper.java:193)
2021-03-26 11:30:49.671 8620-8620/cn.hsl.example E/DEBUG:     at android.app.ActivityThread.main(ActivityThread.java:6718)
2021-03-26 11:30:49.671 8620-8620/cn.hsl.example E/DEBUG:     at java.lang.reflect.Method.invoke(Native Method)
2021-03-26 11:30:49.672 8620-8620/cn.hsl.example E/DEBUG:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
2021-03-26 11:30:49.672 8620-8620/cn.hsl.example E/DEBUG:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2021-03-26 11:30:49.672 8620-8620/cn.hsl.example E/DEBUG: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
2021-03-26 11:30:49.672 8620-8620/cn.hsl.example E/DEBUG:     at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:120)
2021-03-26 11:30:49.673 8620-8620/cn.hsl.example E/DEBUG:     at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:155)
2021-03-26 11:30:49.673 8620-8620/cn.hsl.example E/DEBUG:     at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:31)
2021-03-26 11:30:49.673 8620-8620/cn.hsl.example E/DEBUG:     at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:55)
2021-03-26 11:30:49.673 8620-8620/cn.hsl.example E/DEBUG:     at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:33)
2021-03-26 11:30:49.673 8620-8620/cn.hsl.example E/DEBUG:     at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:33)
2021-03-26 11:30:49.674 8620-8620/cn.hsl.example E/DEBUG:     at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201)
2021-03-26 11:30:49.674 8620-8620/cn.hsl.example E/DEBUG:     at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
2021-03-26 11:30:49.674 8620-8620/cn.hsl.example E/DEBUG:     at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
2021-03-26 11:30:49.674 8620-8620/cn.hsl.example E/DEBUG:     at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
2021-03-26 11:30:49.674 8620-8620/cn.hsl.example E/DEBUG:     at cn.hsl.example.xxxActivity._$_findCachedViewById(Unknown Source:25)
2021-03-26 11:30:49.675 8620-8620/cn.hsl.example E/DEBUG:     at cn.hsl.example.xxxActivity.<init>(ReactionDrumActivity.kt:47)
2021-03-26 11:30:49.675 8620-8620/cn.hsl.example E/DEBUG:     at java.lang.Class.newInstance(Native Method)
2021-03-26 11:30:49.675 8620-8620/cn.hsl.example E/DEBUG:     at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
2021-03-26 11:30:49.675 8620-8620/cn.hsl.example E/DEBUG:     at android.app.Instrumentation.newActivity(Instrumentation.java:1215)
2021-03-26 11:30:49.675 8620-8620/cn.hsl.example E/DEBUG:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2869)
2021-03-26 11:30:49.676 8620-8620/cn.hsl.example E/DEBUG: 	... 11 more
2021-03-26 11:30:49.676 8620-8620/cn.hsl.example E/DEBUG: Back traces ends.
2021-03-26 11:30:49.676 8620-8620/cn.hsl.example E/DEBUG: --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
2021-03-26 11:30:49.676 8620-8620/cn.hsl.example E/DEBUG: meminfo:
2021-03-26 11:30:49.676 8620-8620/cn.hsl.example E/DEBUG: MemTotal:        3744240 kB
    MemFree:           49476 kB
    MemAvailable:    1212412 kB
    Buffers:           11908 kB
    Cached:          1305404 kB
    SwapCached:         8600 kB
    Active:          1594884 kB
    Inactive:         960020 kB
    Active(anon):    1010644 kB
    Inactive(anon):   375444 kB
    Active(file):     584240 kB
    Inactive(file):   584576 kB
    Unevictable:      146832 kB
    Mlocked:          146832 kB

二、解决方案


上述错误就是在 setContentView(R.layout.xxx) 之前调用 findViewById 导致的错误 ;

var texts: Array<TextView> = arrayOf(text0, text1) 代码中的 text0 是布局中的 R.id.text0 组件 , 该位置实际上是调用了 (TextView)findViewById(R.id.text0) 方法 , 但是调用该方法的前提是已经设置了布局 ;

修改方案 :

① 成员变量中不进行赋值 , 使用延迟加载 , lateinit var texts: Array<TextView> ;

② 在 onCreate 方法中 setContentView 方法之后再进行赋值 ;

代码语言:javascript复制
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.xxx)
		
		texts = arrayOf(text0, text1)
    }

0 人点赞