【发布时间】:2019-05-09 08:10:58
【问题描述】:
所以我有来自公共 API 的数据的回收站视图。我的activity_main.xml 仅用于FrameLayout,fragment_main.xml 用于recyclerView,而user_row.xml 用于recycler 视图中的用户行。当我启动应用程序时,它甚至没有加载并关闭并显示错误消息:
“java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'”
这是 MainActivity。它只是用于片段调用
class MainActivity : AppCompatActivity(), BlankFragment.OnFragmentInteractionListener {
lateinit var blankFragment: BlankFragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
blankFragment = BlankFragment.newInstance()
supportFragmentManager
.beginTransaction()
.add(R.id.fragmentContainer,blankFragment)
.addToBackStack(blankFragment.toString())
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit()
}
如您所见,错误是由按钮单击引起的。该按钮用于调用另一个片段
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
detailBtn.setOnClickListener {
detailFragment = DetailFragment.newInstance(idTV.text.toString().toInt())
fragmentManager!!.beginTransaction()
.replace(R.id.fragmentContainer, detailFragment)
.addToBackStack(BlankFragment.toString())
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit()
}
我试过了
val btn: Button = detailBtn!!
但是错误变成了 NullPointerException
这可能是因为 detailBtn 在另一个布局中,但我不确定,也不知道如何修复它。
这是 main_activity.xml 布局
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/fragmentContainer"
android:layout_height="match_parent">
</FrameLayout>
这是 fragment_main.xml 布局
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
这是带有 Button 和 idTextView (idTV)
的 user_row 布局<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:onClick="itemClicked"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:layout_marginLeft="16dp"
android:id="@+id/idTV"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toStartOf="@+id/usernameTV"
tools:text="ID"
android:textSize="25sp"
android:textColor="#D32F2F"
android:padding="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:layout_marginLeft="16dp"
android:id="@+id/usernameTV"
app:layout_constraintStart_toEndOf="@+id/idTV"
app:layout_constraintTop_toTopOf="parent"
tools:text="Userame"
android:textSize="25sp"
android:textColor="#000"
android:padding="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="+"
android:textSize="35sp"
android:layout_marginRight="16dp"
android:id="@+id/detailBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#E91E63"/>
<View
app:layout_constraintTop_toBottomOf="@id/usernameTV"
android:layout_width="match_parent"
android:layout_height="3px"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#000"/>
</android.support.constraint.ConstraintLayout>
【问题讨论】:
-
您从未引用过该按钮;使用
View.findViewById()。 -
@SusmitAgrawal 是 kotlin,不需要 findviewbyId
-
在 Kotlin 中不需要 findviewbyid
标签: android android-fragments kotlin android-recyclerview