Java 使用Firestore中的数据更新片段中的TexView时出现NullPointer异常

Java 使用Firestore中的数据更新片段中的TexView时出现NullPointer异常,java,android,android-fragments,google-cloud-firestore,Java,Android,Android Fragments,Google Cloud Firestore,我试图在片段中更新Firestore中的用户配置文件,但每次运行应用程序时都会出现以下错误: java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)” 我还是个新手,如果我的问题可能会冒犯你,我很抱歉 public class ProfileFragment extends Fragment { TextView personNa

我试图在片段中更新Firestore中的用户配置文件,但每次运行应用程序时都会出现以下错误:

java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)”

我还是个新手,如果我的问题可能会冒犯你,我很抱歉

public class ProfileFragment extends Fragment {

    TextView personName, mAge, bloodGroup, mGender, mHeight, mWeight;
    ImageView settingsButton;

    String userID;

    FirebaseAuth firebaseAuth;
    FirebaseFirestore firebaseFirestore;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.profile_fragment, container, false);

        personName = view.findViewById(R.id.text_person_name);
        bloodGroup = view.findViewById(R.id.blood_group);
        mAge = view.findViewById(R.id.profile_age);
        mGender = view.findViewById(R.id.gender);
        mHeight = view.findViewById(R.id.profile_height);
        mWeight = view.findViewById(R.id.profile_weight);
        settingsButton = view.findViewById(R.id.profile_settings);

        firebaseAuth = FirebaseAuth.getInstance();
        firebaseFirestore = FirebaseFirestore.getInstance();

        // Retrieve UserID
        userID = firebaseAuth.getCurrentUser().getUid();

        DocumentReference documentReference = firebaseFirestore.collection("patients").document(userID);
        documentReference.addSnapshotListener(getActivity(), new EventListener<DocumentSnapshot>() {
            @Override
            public void onEvent(@javax.annotation.Nullable DocumentSnapshot documentSnapshot, @javax.annotation.Nullable FirebaseFirestoreException e) {
                personName.setText(documentSnapshot.getString("Name"));
                bloodGroup.setText(documentSnapshot.getString("BloodGroup"));
                mAge.setText(documentSnapshot.getString("Age"));
                mGender.setText(documentSnapshot.getString("Gender"));
                mHeight.setText(documentSnapshot.getString("Height"));
                mWeight.setText(documentSnapshot.getString("Weight"));

            }
        });



        return view;

    }
}
公共类ProfileFragment扩展了片段{
TextView人名、法师、血族、姆根德、mhweight、mwweight;
图像视图设置按钮;
字符串用户标识;
FirebaseAuth FirebaseAuth;
FirebaseFirestore FirebaseFirestore;
@可空
@凌驾
创建视图时的公共视图(@NonNull LayoutInflater inflater、@Nullable ViewGroup container、@Nullable Bundle savedInstanceState){
视图=充气机。充气(R.layout.profile_碎片,容器,假);
personName=view.findViewById(R.id.text\u person\u name);
血型=view.findViewById(R.id.blood_组);
mAge=view.findviewbyd(R.id.profile\u-age);
mGender=view.findviewbyd(R.id.gender);
mHeight=视图findviewbyd(R.id.剖面高度);
MWweight=视图。findViewById(R.id.剖面图\重量);
settingsButton=view.findViewById(R.id.profile_设置);
firebaseAuth=firebaseAuth.getInstance();
firebaseFirestore=firebaseFirestore.getInstance();
//检索用户ID
userID=firebaseAuth.getCurrentUser().getUid();
DocumentReference DocumentReference=firebaseFirestore.collection(“患者”).document(用户ID);
documentReference.addSnapshotListener(getActivity(),new EventListener()){
@凌驾
public void onEvent(@javax.annotation.Nullable DocumentSnapshot DocumentSnapshot,@javax.annotation.Nullable FirebaseFirestoreException e){
personName.setText(documentSnapshot.getString(“名称”));
bloodGroup.setText(documentSnapshot.getString(“bloodGroup”);
setText(documentSnapshot.getString(“年龄”));
mGender.setText(documentSnapshot.getString(“性别”);
mHeight.setText(documentSnapshot.getString(“高度”);
setText(documentSnapshot.getString(“权重”));
}
});
返回视图;
}
}

您的一个文本视图为空。这意味着您膨胀的XML不包含您要查找的视图的ID。如果没有更多的细节,就无法确定,但是您应该仔细检查代码中的视图ID是否与膨胀的XML中的视图ID完全匹配。

personName
bloodGroup
mAge
mGender
mWheet
还是
mWheet
null?问:具体哪一行(即,哪个变量为“null”)?建议:在
onCreateView()
中设置断点,在调试器(Android Studio?)中逐步执行代码,并检查每个TextView变量(personName、bloodGroup等),直到您确定哪个变量未初始化。谢谢,这里的参考personName=view.findViewById(R.id.text\u person\u name);是不正确的,我做了更正,代码complilesDownvote没有确保OP密切关注堆栈跟踪中的行,并充分利用debugger@FoggyDay谢谢你的反馈。对于未来,我会建议这种有益的反馈比批评反馈更有效,更受欢迎我投了反对票。严厉的反馈无助于让其他人觉得他们想要参与到这个社区的努力中来。我们也欢迎仅仅提出一个修改来改进答案。