【发布时间】:2023-02-15 11:10:16
【问题描述】:
Android Studio 的 Logcat 声明“Caused by: java.lang.NullPointerException: findViewById(R.id.rvMaps) 不能为空“
由于空指针,安装在 Android 模拟器或物理设备上的应用无法打开 例外。
几天来一直试图找到解决方案,但一直找不到。
帮我解决这个问题。 谢谢!
MainActivity.kt 中的代码是:
package eu.tutorials.mymaps
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import eu.tutorials.mymaps.models.Place
import eu.tutorials.mymaps.models.UserMap
class MainActivity : AppCompatActivity() {
private lateinit var rvMaps: RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
rvMaps = findViewById(R.id.rvMaps)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val userMaps = generateSampleData()
// Set layout manager on the recycler view
rvMaps.layoutManager = LinearLayoutManager(this)
// Set adapter on the recycler view
rvMaps.adapter = MapsAdapter(this, userMaps)
}
private fun generateSampleData(): List<UserMap> {
return listOf(
UserMap(
"Memories from University",
listOf(
Place("Branner Hall", "Best dorm at Stanford", 37.426, -122.163),
Place("Gates CS building", "Many long nights in this basement", 37.430, -122.173),
Place("Pinkberry", "First date with my wife", 37.444, -122.170)
)
),
UserMap("January vacation planning!",
listOf(
Place("Tokyo", "Overnight layover", 35.67, 139.65),
Place("Ranchi", "Family visit + wedding!", 23.34, 85.31),
Place("Singapore", "Inspired by \"Crazy Rich Asians\"", 1.35, 103.82)
)),
UserMap("Singapore travel itinerary",
listOf(
Place("Gardens by the Bay", "Amazing urban nature park", 1.282, 103.864),
Place("Jurong Bird Park", "Family-friendly park with many varieties of birds", 1.319, 103.706),
Place("Sentosa", "Island resort with panoramic views", 1.249, 103.830),
Place("Botanic Gardens", "One of the world's greatest tropical gardens", 1.3138, 103.8159)
)
),
UserMap("My favorite places in the Midwest",
listOf(
Place("Chicago", "Urban center of the midwest, the \"Windy City\"", 41.878, -87.630),
Place("Rochester, Michigan", "The best of Detroit suburbia", 42.681, -83.134),
Place("Mackinaw City", "The entrance into the Upper Peninsula", 45.777, -84.727),
Place("Michigan State University", "Home to the Spartans", 42.701, -84.482),
Place("University of Michigan", "Home to the Wolverines", 42.278, -83.738)
)
),
UserMap("Restaurants to try",
listOf(
Place("Champ's Diner", "Retro diner in Brooklyn", 40.709, -73.941),
Place("Althea", "Chicago upscale dining with an amazing view", 41.895, -87.625),
Place("Shizen", "Elegant sushi in San Francisco", 37.768, -122.422),
Place("Citizen Eatery", "Bright cafe in Austin with a pink rabbit", 30.322, -97.739),
Place("Kati Thai", "Authentic Portland Thai food, served with love", 45.505, -122.635)
)
)
)
}
}
我不知道这个问题的确切原因,但我尝试访问 rvMaps 回收器视图 通过它在 MainActivity.kt 中的 id,抛出一个 nullpointerexception 使应用程序崩溃。
【问题讨论】:
标签: android kotlin nullpointerexception logcat findviewbyid