我使用 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像往常一樣初始化時出現此錯誤,請忽略我糟糕的英語!
uj5u.com熱心網友回復:
我建議你在 kotlin 中使用
在您需要同步和重建專案之后。這是你的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)
}
}
}
uj5u.com熱心網友回復:
這實際上是我粗心的錯,我的笨蛋,我忘記在 v24 中更新我的 activity_home.xml :|
mainRvref 沒有參考 layout_v24 xml 檔案中的任何內容,因此請檢查所有布局檔案
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/411481.html
標籤:
