android UninitializedPropertyAccessException

nx7onnlm  于 2023-10-14  发布在  Android
关注(0)|答案(1)|浏览(111)
FATAL EXCEPTION: main
Process: com.example.organizeme, PID: 6281
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.organizeme/com.example.organizeme.SplashActivity}: kotlin.UninitializedPropertyAccessException: lateinit property binding has not been initialized
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
      at android.app.ActivityThread.-wrap12(ActivityThread.java)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:154)
      at android.app.ActivityThread.main(ActivityThread.java:6077)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
  Caused by: kotlin.UninitializedPropertyAccessException: lateinit property binding has not been initialized
      at com.example.organizeme.SplashActivity.onCreate(SplashActivity.kt:31)
      at android.app.Activity.performCreate(Activity.java:6662)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:154) 
      at android.app.ActivityThread.main(ActivityThread.java:6077) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)  
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

我尝试在网上搜索解决错误,但无法得到一个清晰或简洁的解决方案的错误

  • SplashActivity.kt*
package com.example.organizeme

import android.annotation.SuppressLint
import android.graphics.Typeface
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.WindowInsets
import android.view.WindowManager
import com.example.organizeme.databinding.ActivitySplashBinding

@SuppressLint("CustomSplashScreen")
class SplashActivity : AppCompatActivity() {

    private lateinit var binding: ActivitySplashBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            window.insetsController?.hide(WindowInsets.Type.statusBars())
        } else {
            @Suppress("DEPRECATION")
            window.setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN
            )
        }

        val view = binding.root

        setContentView(view)

        val typeFace : Typeface = Typeface.createFromAsset(assets, "carbon bl.otf")
        binding.tvAppName.typeface = typeFace
    }
}
  • activity_splash.xml*
<?xml version="1.0" encoding="utf-8"?>
<!--suppress XmlUnusedNamespaceDeclaration -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:background="@drawable/splash_screen"
    tools:context=".SplashActivity">

    <TextView
        android:id="@+id/tvAppName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/app_name"
        android:textColor="@android:color/white"
        android:textSize="@dimen/splash_screen_title_text_size"
        android:textStyle="bold" />
</LinearLayout>
  • Android Manifest.xml*
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.OrganizeMe"
        tools:targetApi="31">
        <activity
            android:name=".SplashActivity"
            android:screenOrientation="fullSensor"
            android:exported="true"
            android:label="@string/title_activity_splash"
            android:theme="@style/Theme.OrganizeMe" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            </activity>
        <activity
            android:name=".MainActivity"
            android:exported="true">
        </activity>
    </application>

</manifest>
hkmswyz6

hkmswyz61#

stacktrace中的第一行声明:“lateinit属性绑定尚未初始化”。在SplashActivity内部的onCreate方法中,没有对绑定变量进行赋值。
documentation中可以看到,需要使用inflate方法来初始化绑定变量。

class SplashActivity : AppCompatActivity() {

    private lateinit var binding: ActivitySplashBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySplashBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)
   }
}

还请注意,在原始代码中,您调用了两次setContentView。

相关问题