当我在Android脚本中定义一个值时,它会给我一个错误



我是Android和Kotlin的新手,我想制作一个应用程序,它可以让你随机组队。我试着用SharedPreferences存储玩家的名字,并必须定义一些值(我不明白什么是值(,但当我定义它们时,它会崩溃应用程序。我知道proplem与值有关,因为如果我删除它们,应用程序运行良好。

enterName脚本


import android.content.Context
import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
class enterName : AppCompatActivity() {
var namesButton = findViewById<Button>(R.id.button2)
var namesText = findViewById<TextView>(R.id.namesText)
var outputText = findViewById<TextView>(R.id.outputText)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_enter_name)
namesButton.setOnClickListener {
saveData()
}
}
private fun saveData() {
val insertedText = namesText.text.toString()
outputText.text = insertedText
val sharedPreferences = getSharedPreferences("namesPref", Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
editor.apply {
putString("namesPref", insertedText)
}.apply()
}
}

活动脚本

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".enterName">
<EditText
android:id="@+id/namesText"
android:layout_width="379dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:ems="10"
android:hint="Enter Player Names"
android:inputType="textPersonName"
android:maxLines="3"
android:minHeight="48dp"
android:singleLine="false"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="286dp"
android:layout_height="41dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="@string/separateNames"
android:textAlignment="center"
android:textColor="#5A5A5A"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/namesText" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="NEXT"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/outputText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="hi"
android:textColor="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button2" />
</androidx.constraintlayout.widget.ConstraintLayout>

错误/日志

2022-04-04 19:55:37.994 6997-6997/? I/.teamrandomize: Late-enabling -Xcheck:jni
2022-04-04 19:55:38.000 6997-6997/at.thebuckey_.teamrandomizer E/.teamrandomize: Unknown bits set in runtime_flags: 0x8000
2022-04-04 19:55:37.994 6997-6997/? I/.teamrandomize: Late-enabling -Xcheck:jni
2022-04-04 19:55:38.000 6997-6997/at.thebuckey_.teamrandomizer E/.teamrandomize: Unknown bits set in runtime_flags: 0x8000
2022-04-04 19:55:37.994 6997-6997/? I/.teamrandomize: Late-enabling -Xcheck:jni
2022-04-04 19:55:38.000 6997-6997/at.thebuckey_.teamrandomizer E/.teamrandomize: Unknown bits set in runtime_flags: 0x8000
2022-04-04 19:55:38.122 6997-7031/at.thebuckey_.teamrandomizer D/libEGL: Emulator has host GPU support, qemu.gles is set to 1.
2022-04-04 19:55:38.122 6997-7031/at.thebuckey_.teamrandomizer W/libc: Unable to set property "qemu.gles" to "1": connection failed; errno=13 (Permission denied)
2022-04-04 19:55:38.118 6997-6997/at.thebuckey_.teamrandomizer W/RenderThread: type=1400 audit(0.0:42): avc: denied { write } for name="property_service" dev="tmpfs" ino=6596 scontext=u:r:untrusted_app:s0:c144,c256,c512,c768 tcontext=u:object_r:property_socket:s0 tclass=sock_file permissive=0 app=at.thebuckey_.teamrandomizer
2022-04-04 19:55:38.127 6997-7031/at.thebuckey_.teamrandomizer D/libEGL: loaded /vendor/lib64/egl/libEGL_emulation.so
2022-04-04 19:55:38.128 6997-7031/at.thebuckey_.teamrandomizer D/libEGL: loaded /vendor/lib64/egl/libGLESv1_CM_emulation.so
2022-04-04 19:55:38.129 6997-7031/at.thebuckey_.teamrandomizer D/libEGL: loaded /vendor/lib64/egl/libGLESv2_emulation.so
2022-04-04 19:55:38.153 6997-6997/at.thebuckey_.teamrandomizer W/.teamrandomize: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2022-04-04 19:55:38.153 6997-6997/at.thebuckey_.teamrandomizer W/.teamrandomize: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2022-04-04 19:55:38.191 6997-7029/at.thebuckey_.teamrandomizer D/HostConnection: HostConnection::get() New Host Connection established 0x7b23c96400, tid 7029
2022-04-04 19:55:38.193 6997-7029/at.thebuckey_.teamrandomizer D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit ANDROID_EMU_sync_buffer_data GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_async_frame_commands ANDROID_EMU_gles_max_version_3_0 
2022-04-04 19:55:38.194 6997-7029/at.thebuckey_.teamrandomizer W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2022-04-04 19:55:38.195 6997-7029/at.thebuckey_.teamrandomizer D/EGL_emulation: eglCreateContext: 0x7b23c965e0: maj 3 min 0 rcv 3
2022-04-04 19:55:38.195 6997-7029/at.thebuckey_.teamrandomizer D/EGL_emulation: eglMakeCurrent: 0x7b23c965e0: ver 3 0 (tinfo 0x7b23c18f00)
2022-04-04 19:55:38.203 6997-7029/at.thebuckey_.teamrandomizer W/Gralloc3: mapper 3.x is not supported
2022-04-04 19:55:38.205 6997-7029/at.thebuckey_.teamrandomizer D/HostConnection: createUnique: call
2022-04-04 19:55:38.206 6997-7029/at.thebuckey_.teamrandomizer D/HostConnection: HostConnection::get() New Host Connection established 0x7b23c96720, tid 7029
2022-04-04 19:55:38.206 6997-7029/at.thebuckey_.teamrandomizer D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit ANDROID_EMU_sync_buffer_data GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_async_frame_commands ANDROID_EMU_gles_max_version_3_0 
2022-04-04 19:55:38.206 6997-7029/at.thebuckey_.teamrandomizer D/eglCodecCommon: allocate: Ask for block of size 0x1000
2022-04-04 19:55:38.207 6997-7029/at.thebuckey_.teamrandomizer D/eglCodecCommon: allocate: ioctl allocate returned offset 0x1fef08000 size 0x8000
2022-04-04 19:55:38.210 6997-7029/at.thebuckey_.teamrandomizer D/EGL_emulation: eglMakeCurrent: 0x7b23c965e0: ver 3 0 (tinfo 0x7b23c18f00)
2022-04-04 19:55:39.253 6997-6997/at.thebuckey_.teamrandomizer I/AssistStructure: Flattened final assist data: 1472 bytes, containing 1 windows, 8 views
2022-04-04 19:55:40.804 6997-6997/at.thebuckey_.teamrandomizer W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@2616b45
2022-04-04 19:55:40.808 6997-6997/at.thebuckey_.teamrandomizer D/AndroidRuntime: Shutting down VM
2022-04-04 19:55:40.809 6997-6997/at.thebuckey_.teamrandomizer E/AndroidRuntime: FATAL EXCEPTION: main
Process: at.thebuckey_.teamrandomizer, PID: 6997
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{at.thebuckey_.teamrandomizer/at.thebuckey_.teamrandomizer.enterName}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:163)
at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:174)
at android.content.Context.obtainStyledAttributes(Context.java:738)
at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:848)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:815)
at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:640)
at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:259)
at at.thebuckey_.teamrandomizer.enterName.<init>(enterName.kt:12)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
at android.app.Instrumentation.newActivity(Instrumentation.java:1243)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3182)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
2022-04-04 19:55:40.819 6997-6997/at.thebuckey_.teamrandomizer I/Process: Sending signal. PID: 6997 SIG: 9

我不知道什么"值";您正在谈论,但您的应用程序因以下错误而崩溃:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{at.thebuckey_.teamrandomizer/at.thebuckey_.teamrandomizer.enterName}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference

也就是说你试图在一个实际上为null的CCD_ 3上调用CCD_。正因为如此:

class enterName : AppCompatActivity() {
var namesButton = findViewById<Button>(R.id.button2)
var namesText = findViewById<TextView>(R.id.namesText)
var outputText = findViewById<TextView>(R.id.outputText)

Activity中调用findViewById需要其View层次结构(因此您可以在其中找到要搜索的视图(,长话短说,该调用就是尝试使用"活动"的Context。而且它在构造时没有,所以上下文为null

您需要在onCreate或更高版本中进行所有视图查找,此时您确实具有Context(用于视图查找,特别是在创建视图之后,例如使用setContentView(。改为:

class enterName : AppCompatActivity() {
lateinit var namesButton: Button
lateinit var namesText: TextView
lateinit var outputText: TextView

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_enter_name)
namesButton = findViewById<Button>(R.id.button2)
namesText = findViewById<TextView>(R.id.namesText)
outputText = findViewById<TextView>(R.id.outputText)
namesButton.setOnClickListener {
saveData()
}
}

lateinit允许您声明那些顶级变量,但init它们later(即,当您拥有所需的Context时(


;值";事情是,当你把数据存储在共享的pref中时,你需要一个键/值对——键是你用来存储一段数据的标识符,值是数据本身,仅此而已。你在这里做:

editor.apply {
putString("namesPref", insertedText)
}.apply()

这就是使用查找关键字"namesPref"来存储insertedText(值(。因此,您可以稍后使用getString("namesPref")检索该值。由于这也是您的SharedPreferences商店的名称,我猜您想使用不同的名称进行查找,但这是的基础

相关内容

最新更新