提问人:George_Sari 提问时间:4/9/2023 最后编辑:George_Sari 更新时间:4/9/2023 访问量:155
我在尝试访问按钮或 AppCompatEditText 时遇到问题
I have a problem when trying to access a button or an AppCompatEditText
问:
我尝试使用 findViewById() 函数访问按钮或 AppCompatEditText
Kotlin 代码:
package com.example.qrecycle
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.WindowManager
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.widget.AppCompatEditText
class RegisterActivity : AppCompatActivity() {
// val fullNameInput : AppCompatEditText = findViewById(R.id.editTextFullName)
// val emailInput : AppCompatEditText = findViewById(R.id.editTextEmail)
// val passwordInput : AppCompatEditText = findViewById(R.id.editTextPassword)
// val confirmPasswordInput : AppCompatEditText = findViewById(R.id.editTextConfirmPassword)
override fun onCreate(savedInstanceState: Bundle?) {
val registerBtn : Button = findViewById(R.id.registerBtn)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_register)
@Suppress("DEPRECATION")
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
)
// Toast.makeText(this, "I WORK!!", Toast.LENGTH_SHORT).show()
registerBtn.setOnClickListener {
Toast.makeText(this, "I work", Toast.LENGTH_SHORT).show()
}
}
}
XML代码:
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
android:gravity="center"
android:background="@drawable/background_gradient"
tools:context=".RegisterActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:fontFamily="@font/montserrat_alternates"
android:text="@string/registerMsg"
android:textSize="30sp"
android:textStyle="bold"
android:textColor="@color/white" />
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:elevation="20dp"
app:cardBackgroundColor="@color/backgroundColor"
app:cardCornerRadius="25dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center" >
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginTop="20dp"
android:hint="@string/fullNameText"
app:endIconMode="clear_text"
android:textColorHint="@color/blackWithLowerOpacity"
app:boxBackgroundMode="none">
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/editTextFullName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="@drawable/shape_edit_text_rounded"
android:gravity="start"
android:inputType="textCapWords"
android:textSize="18sp"
tools:ignore="SpeakableTextPresentCheck,VisualLintTextFieldSize" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:hint="@string/emailText"
app:endIconMode="clear_text"
android:textColorHint="@color/blackWithLowerOpacity"
app:boxBackgroundMode="none" >
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/editTextEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="@drawable/shape_edit_text_rounded"
android:gravity="start"
android:inputType="textEmailAddress"
android:textSize="18sp"
tools:ignore="SpeakableTextPresentCheck,VisualLintTextFieldSize" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:hint="@string/passwordText"
app:endIconMode="clear_text"
android:textColorHint="@color/blackWithLowerOpacity"
app:boxBackgroundMode="none" >
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="@drawable/shape_edit_text_rounded"
android:gravity="start"
android:inputType="textPassword"
android:textSize="18sp"
tools:ignore="SpeakableTextPresentCheck,VisualLintTextFieldSize" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
android:hint="@string/confirmPasswordText"
app:endIconMode="clear_text"
android:textColorHint="@color/blackWithLowerOpacity"
app:boxBackgroundMode="none" >
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/editTextConfirmPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="@drawable/shape_edit_text_rounded"
android:gravity="start"
android:inputType="textPassword"
android:textSize="18sp"
tools:ignore="SpeakableTextPresentCheck,VisualLintTextFieldSize" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/registerBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="40dp"
android:background="@drawable/shape_button_dark_blue_rounded"
android:fontFamily="@font/lobster"
android:letterSpacing=".05"
android:textSize="22sp"
android:text="@string/registerBtnText"
android:textColor="@color/white"
android:textAllCaps="false"
tools:ignore="VisualLintButtonSize" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
当我尝试运行我的应用程序并进入该活动时,应用程序崩溃,我收到此错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.qrecycle, PID: 14505
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.qrecycle/com.example.qrecycle.RegisterActivity}: 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:3682)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3942)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:109)
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:2345)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:233)
at android.os.Looper.loop(Looper.java:344)
at android.app.ActivityThread.main(ActivityThread.java:8212)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:584)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1034)
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:191)
at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:174)
at android.content.Context.obtainStyledAttributes(Context.java:804)
at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:922)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:889)
at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:691)
at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:264)
at com.example.qrecycle.RegisterActivity.<init>(RegisterActivity.kt:13)
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:1288)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3669)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3942)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:109)
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:2345)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:233)
at android.os.Looper.loop(Looper.java:344)
at android.app.ActivityThread.main(ActivityThread.java:8212)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:584)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1034)
I/Process: Sending signal. PID: 14505 SIG: 9
我还收到以下消息:
V/AutofillManager: requestHideFillUi(null): anchor = null
V/AutofillManager: requestHideFillUi(null): anchor = null
V/TextView: notifyAutoFillManagerAfterTextChanged
V/AutofillManager: notifyValueChanged(1073741824): ignoring on state UNKNOWN
多次
我是 android 开发的新手,所以欢迎任何让我的代码更好的评论。
我试过这些:
-
private lateinit val fullNameInput : AppCompatEditText val fullNameInputString = fullNameInput.text.toString()
-
val fullNameInput : AppCompatEditText = findViewById(R.id.editTextFullName)
-
private val fullNameInput : AppCompatEditText = findViewById(R.id.editTextFullName)
答:
0赞
Zapbroob
4/9/2023
#1
问题是,在调用“setContentView()”方法之前,您正在尝试访问属于“View”类的UI元素。您可以在 setContentView() 方法之前执行所需的任何代码,只是它不引用尚未设置的 View(部分)。因此,请在调用“setContentView()”后尝试使用“findViewById()”方法。
评论
0赞
George_Sari
4/9/2023
我尝试了您的解决方案,但仍然有完全相同的问题。谢谢
0赞
Zapbroob
4/10/2023
请确保在清单文件中使用正确的包名称和活动名称正确声明 RegisterActivity。
0赞
George_Sari
4/10/2023
在使用注释掉变量进行调试后,我发现此问题仅在 emailTextInput 变量未注释时发生,而我仍然没有弄清楚为什么会发生这种情况
评论