Kotlin:createDisableExceptionQarthFile方法失败



我正在尝试为布局动态设置文本。

示例代码:Start.kt:

import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.Context.LOCATION_SERVICE
import android.content.pm.PackageManager
import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.example.basic.databinding.StartBinding
class Start : Fragment() {
override fun onCreate(savedInstanceState: Bundle?){
super.onCreate(savedInstanceState)
val binding : StartBinding = setContentView(MainActivity(), R.layout.start)
binding.textView.setText("Hello World")
}
}

start.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Start">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:text="Current GPS Location"
android:textColor="#000"
android:textSize="30sp"
android:textStyle="bold" />
...

Gradle构建正常结束,但当我在手机上打开此页面时,应用程序崩溃并出现此错误:

[qarth_debug:]  get PatchStore::createDisableExceptionQarthFile method fail.
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference

如何修理?

Gradle还可以,但page"启动";崩溃。

get PatchStore::createDisableExceptionQarthFile method fail.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.basic, PID: 13619
kotlin.UninitializedPropertyAccessException: lateinit property binding has not been initialized
at com.example.basic.Start.onViewCreated(Start.kt:56)

第56行是这样的:binding.textView.setText("Hello World")

所以。。。我将阅读你链接到这里的教程。

请参阅的碎片教程

了解碎片是如何工作的

查看绑定请参阅此

关于Kotlin,请参阅Kotlin Offical Doc

设置片段视图fragment覆盖onCreateView,然后在onViewCreated回调中更新初始状态的ui。

class Start : Fragment() {
lateinit var binding : StartBinding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding  = StartBinding.inflate(layoutInflater,container,false) 
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.textView.setText("Hello World")
}
}

相关内容

  • 没有找到相关文章

最新更新