我在应用程序的导航抽屉中有一个textview。最初加载相关xml时,texview(显示用户名和姓氏)运行良好,没有错误。但是,导航到下一个活动并立即返回上一个活动后,应用程序崩溃,并出现nullpointer异常。为什么它最初会成功加载,但是向后导航时会崩溃。因为我已经进行了空检查,但这只是将textView设置为空白,但可以防止崩溃。
我的导航头xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="117dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
app:srcCompat="@drawable/logo" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:textSize="24sp" />
<TextView
android:id="@+id/textView40"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp" />
</LinearLayout>
</LinearLayout>
我引用导航标题的部分布局xml:
<com.google.android.material.navigation.NavigationView
android:id="@+id/mydeals_seller_nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/navigation_header"
android:fitsSystemWindows="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:menu="@menu/seller_menu"/>
引用活动中的textview:docref = FirebaseFirestore.getInstance()
docref.collection("Users").whereEqualTo("uid", userid).get()
.addOnSuccessListener { value->
if (value != null ) {
for (document in value!!) {
val user_name = document.getString("name")!!
val user_surname = document.getString("surname")!!
if(textView15 != null || textView40 != null){
menu_name = findViewById(R.id.textView15)!!
menu_surname = findViewById(R.id.textView40)!!
menu_name.setText("$user_name ")
menu_surname.setText(user_surname)
}
}
} else {
Log.d("Firestore_error", "No Data")
}
}
每个活动我需要单独的导航标题和Textview吗?
编辑在API 22上对其进行进一步测试,此问题不存在。有什么建议吗?
docref.collection(“ Users”)。whereEqualTo(“ uid”,userid).get().addOnSuccessListener {值->
if (value != null ) {
for (document in value!!) {
val user_name = document.getString("name")!!
val user_surname = document.getString("surname")!!
if(textView15 != null || textView40 != null){
menu_name = findViewById(R.id.textView15)!!
menu_surname = findViewById(R.id.textView40)!!
menu_name.setText("$user_name ")
menu_surname.setText(user_surname)
}
}
} else {
Log.d("Firestore_error", "No Data")
}
}
将这些行放入onResume()方法中,看看会发生什么。
而不是在活动视图上调用findViewById,您应该像在标题视图上那样调用它:
docref.collection("Users").whereEqualTo("uid", userid).get()
.addOnSuccessListener { value->
if (value != null ) {
for (document in value!!) {
val user_name = document.getString("name")!!
val user_surname = document.getString("surname")!!
// This gets the first header that is attached to the
// navigation view
val header = mydeals_seller_nav_view.getHeaderView(0)
menu_name = header.findViewById(R.id.textView15)!!
menu_surname = header.findViewById(R.id.textView40)!!
menu_name.setText("$user_name ")
menu_surname.setText(user_surname)
}
}
} else {
Log.d("Firestore_error", "No Data")
}
}
希望这些修复程序解决您的问题