在 null 上调用的 ListView ID? (科特林)
ListView ID called on null? (Kotlin)
我是 Kotlin 的新手,我正在尝试构建一个 HomePage
,其中有一个 BottomNavigation
和 3 Fragment
个页面,但在其中一个页面中我设置了一个 ListView
每当我调用 ID 时,它都会给出以下错误 (Caused by: java.lang.IllegalStateException: categoryListView must not be null
)
这是我在我的主页中调用它的方式,也就是内容出现的地方:
class HomePage : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
lateinit var adapter : CategoryAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home_page)
adapter = CategoryAdapter(this, DataService.categories)
categoryListView.adapter = adapter
navView.setNavigationItemSelectedListener(this)
bottomNavigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
replaceFragment(HomeFragment())
}
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when(item.itemId) {
R.id.Bottom_Nav_Home -> {
println("Home pressed")
replaceFragment(HomeFragment())
return@OnNavigationItemSelectedListener true
}
R.id.Bottom_Nav_Notifs -> {
println("Notification pressed")
replaceFragment(NotifsFragment())
return@OnNavigationItemSelectedListener true
}
R.id.Bottom_Nav_List -> {
println("List pressed")
replaceFragment(ListFragment())
return@OnNavigationItemSelectedListener true
}
}
false
}
private fun replaceFragment(fragment: Fragment) {
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.NavPageFragment, fragment)
fragmentTransaction.commit()
}
}
我做的对吗?或者我应该在别处调用 categoryListView 吗?因为我在我的 ListFragment
中尝试过,这是我的 activity 代码所在的位置,它给出了上下文错误
片段 activity 的外观如下:
<?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:id="@+id/Bottom_List"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.ListFragment">
<ListView
android:id="@+id/categoryListView"
android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
您正试图在 activity 中解析 categoryListView
但它是(据我了解)片段的一部分。这根本行不通。
CategoryAdapter
和 ListView
应该在你的 HomeFragment
class 中使用,这样就可以了。
所以将这段代码移到您的片段中:
adapter = CategoryAdapter(this, DataService.categories)
categoryListView.adapter = adapter
更新
我会把它移到 onViewCreated()
方法中。在那里,您可以简单地依赖一个事实,即有一个带有上下文的可用视图。
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
adapter = CategoryAdapter(view.context, DataService.categories)
categoryListView.adapter = adapter // categoryListView will be not null here
}
我是 Kotlin 的新手,我正在尝试构建一个 HomePage
,其中有一个 BottomNavigation
和 3 Fragment
个页面,但在其中一个页面中我设置了一个 ListView
每当我调用 ID 时,它都会给出以下错误 (Caused by: java.lang.IllegalStateException: categoryListView must not be null
)
这是我在我的主页中调用它的方式,也就是内容出现的地方:
class HomePage : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
lateinit var adapter : CategoryAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home_page)
adapter = CategoryAdapter(this, DataService.categories)
categoryListView.adapter = adapter
navView.setNavigationItemSelectedListener(this)
bottomNavigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
replaceFragment(HomeFragment())
}
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when(item.itemId) {
R.id.Bottom_Nav_Home -> {
println("Home pressed")
replaceFragment(HomeFragment())
return@OnNavigationItemSelectedListener true
}
R.id.Bottom_Nav_Notifs -> {
println("Notification pressed")
replaceFragment(NotifsFragment())
return@OnNavigationItemSelectedListener true
}
R.id.Bottom_Nav_List -> {
println("List pressed")
replaceFragment(ListFragment())
return@OnNavigationItemSelectedListener true
}
}
false
}
private fun replaceFragment(fragment: Fragment) {
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.NavPageFragment, fragment)
fragmentTransaction.commit()
}
}
我做的对吗?或者我应该在别处调用 categoryListView 吗?因为我在我的 ListFragment
中尝试过,这是我的 activity 代码所在的位置,它给出了上下文错误
片段 activity 的外观如下:
<?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:id="@+id/Bottom_List"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.ListFragment">
<ListView
android:id="@+id/categoryListView"
android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
您正试图在 activity 中解析 categoryListView
但它是(据我了解)片段的一部分。这根本行不通。
CategoryAdapter
和 ListView
应该在你的 HomeFragment
class 中使用,这样就可以了。
所以将这段代码移到您的片段中:
adapter = CategoryAdapter(this, DataService.categories)
categoryListView.adapter = adapter
更新
我会把它移到 onViewCreated()
方法中。在那里,您可以简单地依赖一个事实,即有一个带有上下文的可用视图。
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
adapter = CategoryAdapter(view.context, DataService.categories)
categoryListView.adapter = adapter // categoryListView will be not null here
}