提问人:krish cyber 提问时间:1/14/2022 更新时间:1/14/2022 访问量:548
java.lang.NullPointerException: mainRvRef 不能为 null ,其中 mainRvRef 引用的是 reyclerView
java.lang.NullPointerException: mainRvRef must not be null ,where mainRvRef is referencing a reyclerView
问:
我使用 findViewById 引用了 mainRvRef, 但我仍然收到错误,因为它不能为 null。我不认为这是一个正常的NullPointerException,请帮帮我!
这是我的 HomeActivity 类
package com.example.loginkt
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.floatingactionbutton.FloatingActionButton
class HomeActivity:AppCompatActivity() {
var questions = ArrayList<QuestionFB>()
init {
for(i:Int in 1..10){
questions.add(QuestionFB(-1,"cse $i","krishna $i","$i. multiply $i * ${i*i}","maths $i", listOf("s","Sas")))
}
} // for testing
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
val fabR = findViewById<FloatingActionButton>(R.id.addQnFab)
val mainRvRef = findViewById<RecyclerView>(R.id.mainRv)
Log.v("Question", questions[0].solution[0])
mainRvRef.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false)
// ABOVE LINE MAKE IT'S SHOWING "mainRvRef can't be null"
mainRvRef.adapter = MainAdapters(questions,this)
fabR.setOnClickListener{
val intent = Intent(this, AskActivity::class.java)
startActivity(intent)
}
}
}
这是我activity_home.xml:
<?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">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/addQnFab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:clickable="true"
android:backgroundTint="@color/pBlue_2"
android:focusable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:src="@drawable/add_icons"
app:maxImageSize="50dp"
android:contentDescription="@string/add_question" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/mainRv"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是我的堆栈跟踪
V/Question: s
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.loginkt, PID: 7231
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.loginkt/com.example.loginkt.HomeActivity}: java.lang.NullPointerException: mainRvRef must not be null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
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: mainRvRef must not be null
at com.example.loginkt.HomeActivity.onCreate(HomeActivity.kt:25)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
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)
I/Process: Sending signal. PID: 7231 SIG: 9
我不知道为什么这个错误在我像往常一样初始化时出现,
请忽略我糟糕的英语!mainRvRef
答:
1赞
Kotlin Learner
1/14/2022
#1
我建议你在 kotlin 中使用 viewbinding。这将简化您的ID参考。如果使用 viewbinding,则代码将如下所示
class HomeActivity:AppCompatActivity() {
private lateinit var binding: ActivityHomeBinding
var questions = ArrayList<QuestionFB>()
init {
for(i:Int in 1..10){
questions.add(QuestionFB(-1,"cse $i","krishna $i","$i. multiply $i * ${i*i}","maths $i", listOf("s","Sas")))
}
} // for testing
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityHomeBinding.inflate(layoutInflater)
setContentView(binding.root)
Log.v("Question", questions[0].solution[0])
binding.mainRvRef.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false)
// ABOVE LINE MAKE IT'S SHOWING "mainRvRef can't be null"
binding.mainRvRef.adapter = MainAdapters(questions,this)
binding.fabR.setOnClickListener{
val intent = Intent(this, AskActivity::class.java)
startActivity(intent)
}
}
}
根据ID名称更改您的ID
更新
您需要在 build.gradle 中添加这些行
buildFeatures {
viewBinding true
}
在需要同步并重新生成项目之后。这是你的例子mainactivity.kt
package com.example.loginkt
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.loginkt.databinding.ActivityMainBinding
import com.google.firebase.auth.FirebaseAuth
lateinit var emailG: String
lateinit var passG: String
lateinit var auth: FirebaseAuth
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private fun firebaseLogin(email: String, pass: String) {
if (email != "" || pass != "") {
auth.signInWithEmailAndPassword(email, pass).addOnCompleteListener { task ->
if (task.isSuccessful) {
getUser()
val intent = Intent(this, HomeActivity::class.java)
startActivity(intent)
finish()
}
}.addOnFailureListener { exception ->
Toast.makeText(applicationContext, exception.localizedMessage, Toast.LENGTH_LONG)
.show()
}
} else {
Toast.makeText(applicationContext, "Enter all the fields", Toast.LENGTH_LONG).show()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
auth = FirebaseAuth.getInstance()
title = "Login"
binding = ActivityMainBinding.inflate(layoutInflater)
super.onCreate(savedInstanceState)
setContentView(binding.root)
binding.SupTB.setOnClickListener {
val intent = Intent(this, RegisterActivity::class.java)
startActivity(intent)
}
binding.loginBtn.setOnClickListener {
emailG = binding.emailTxt.text.toString()
passG = binding.passwordTxt.text.toString()
firebaseLogin(emailG, passG)
}
}
}
评论
0赞
krish cyber
1/14/2022
未解析的引用 ActivityHomeBinder,我发现没有要导入🙂的内容
0赞
Kotlin Learner
1/14/2022
你能分享你的项目吗?我可以帮你
0赞
krish cyber
1/14/2022
github.com/krish-dev-7/United-versity repo 链接
0赞
Kotlin Learner
1/14/2022
您可以在此处添加链接。
0赞
Kotlin Learner
1/14/2022
@krishcyber查看我更新的答案
-1赞
krish cyber
1/14/2022
#2
这实际上是我粗心大意的错,我的愚蠢,我忘记在 v24 中更新我的activity_home.xml:|
mainRvref
未引用 XML 文件中的任何内容layout_v24因此请检查所有布局文件
评论