【发布时间】:2022-01-14 11:36:01
【问题描述】:
我使用 findViewById 引用了 mainRvRef, 但我仍然收到错误,因为它不能为空。我不认为这是一个正常的 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 时会出现这个错误,
请忽略我糟糕的英语!
【问题讨论】:
标签: java android kotlin android-recyclerview nullpointerexception