导航抽屉文本视图空指针异常



我的应用程序的导航抽屉里有一个TextView。最初加载相关 xml 时,TextView(显示用户名和姓氏(运行良好,没有错误。但是,在导航到下一个活动并立即返回到上一个活动后,应用程序崩溃并显示NullPointerException。为什么它最初成功加载,但在导航时崩溃?我已经进行了空检查,但这只会将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 上进一步测试,此问题不会出现。有什么建议吗?

与其在活动视图上调用 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")
}
} 

希望这些修复可以解决您的问题

menu_name = findViewById(R.id.textView15)!!
menu_surname = findViewById(R.id.textView40)!!
menu_name.setText("$user_name ")
menu_surname.setText(user_surname)

将这些行替换为:

val headerView = mydeals_seller_nav_view.getHeaderView(0)
val menu_name = headerView.textView15
val menu_surname = headerView.textView40
menu_name.setText("$user_name ")
menu_surname.setText(user_surname)

请尝试这个... 您必须将该值存储在首选项中,并将首选项值设置为文本视图或在onResume((方法中初始化firbase.instance。

经过大量试验和错误,该错误似乎是Firestore查询问题。 根据我上面的查询,我正在使用一个循环,其中useridID 与经过身份验证的用户匹配。 由于某种原因,在活动之间移动时,它会导致空指针。 我不知道为什么,但是将上面的查询更改为如下所示的单次读取解决了该问题:

docref = FirebaseFirestore.getInstance()
docref.collection("Users").document(userid).get()
.addOnSuccessListener { value->
if (value != null ) {
val user_name = value.getString("name")!!
val user_surname = value.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")
}
}

就这样,不再NullPointerException了,如果Firestore专家能够解释为什么会发生这种情况,那就太好了。

最新更新