我已经到处搜索并尝试了他们的方法来调用虚拟方法,但是结果仍然是一样的。请帮帮我,我不知道我的错误在哪里。
当我在手机上运行应用程序时,它会引发以下异常:
尝试调用虚拟方法
“Java.Lang.StrugMyPosial.Subjix.GETUsReId()”的空值
对象引用
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//FirebaseAuth
firebaseAuth = FirebaseAuth.getInstance();
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
userID = user.getUid();
}
//Realtime Firebase
firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference newData = firebaseDatabase.getReference();
//Display data
newData.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
try {
showData(dataSnapshot);
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
方法ShowData()
private void showData(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
studentInformation studentData = new studentInformation();
studentData.setUserID(ds.child(userID).getValue(studentInformation.class).getUserID());
studentData.setStudentFullName(ds.child(userID).getValue(studentInformation.class).getStudentFullName());
studentData.setStudentID(ds.child(userID).getValue(studentInformation.class).getStudentID());
studentData.setEmail(ds.child(userID).getValue(studentInformation.class).getEmail());
studentData.setCampus(ds.child(userID).getValue(studentInformation.class).getCampus());
studentData.setCourse(ds.child(userID).getValue(studentInformation.class).getCourse());
studentData.setIntake(ds.child(userID).getValue(studentInformation.class).getIntake());
}
}
类studentinformation()。
package mypackage;
import com.google.firebase.database.IgnoreExtraProperties;
@IgnoreExtraProperties
public class studentInformation {
private String userID;
private String studentFullName;
private long studentID;
private String email;
private String campus;
private String course;
private String intake;
studentInformation() {
}
studentInformation(String userID, String studentFullName, long studentID, String email, String campus, String course, String intake) {
this.userID = userID;
this.studentFullName = studentFullName;
this.studentID = studentID;
this.email = email;
this.campus = campus;
this.intake = intake;
}
public String getStudentFullName() {
return studentFullName;
}
public void setStudentFullName(String studentFullName) {
this.studentFullName = studentFullName;
}
public long getStudentID() {
return studentID;
}
public void setStudentID(long studentID) {
this.studentID = studentID;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getCampus() {
return campus;
}
public void setCampus(String campus) {
this.campus = campus;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public String getIntake() {
return intake;
}
public void setIntake(String intake) {
this.intake = intake;
}
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
}
FireBase数据库:
我导出了它,然后从json改为txt。
{
"main" : {
"home" : {
"announcement-date" : "jkljlkjdaj",
"campus" : {
"unikl-miit" : {
"course" : {
"bcem" : {
"courseName" : "BCEM"
}
}
},
"unikl-mimet" : {
"course" : {
"bse" : {
"courseName" : "BSE"
}
}
}
}
}
},
"student" : {
"6LKSe5oqfwgrkEV57NXQekDvNhl1" : {
"campus" : "UniKL MIIT",
"course" : "Diploma In Animation",
"email" : "hazman@gmail.com",
"intake" : "55",
"studentFullName" : "haMan",
"studentID" : 52213116153,
"userID" : "6LKSe5oqfwgrkEV57NXQekDvNhl1"
},
"evPFfkE1VtObUlBNz8pizSNKt662" : {
"campus" : "UniKL MIIT",
"course" : "Bachelor of Multimedia Technology (Hons) in Interactive Multimedia Design (BIMD)",
"email" : "fakyou@gmshit.com",
"intake" : "69",
"studentFullName" : "Adam",
"studentID" : 52213116190,
"userID" : "evPFfkE1VtObUlBNz8pizSNKt662"
},
"rcUgnUBqd4Xb3AxmngE3OD1BBpd2" : {
"campus" : "UniKL MIIT",
"email" : "hazman45@gmail.com",
"intake" : "52/49/4",
"studentFullName" : "jwjajs",
"studentID" : 52213116153,
"userID" : "rcUgnUBqd4Xb3AxmngE3OD1BBpd2"
},
"z46FjWQE8TRYiJxIexMkrp6gSYv2" : {
"campus" : "UniKL MIIT",
"email" : "zoowake97@gmail.com",
"intake" : "16/9/18",
"studentFullName" : "Eisom",
"studentID" : 52213116132,
"userID" : "z46FjWQE8TRYiJxIexMkrp6gSYv2"
}
}
}
问题[更新]:
//FirebaseAuth
firebaseAuth = FirebaseAuth.getInstance();
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
userID = user.getUid();
DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference("student");
DatabaseReference currentUserRef = usersRef.child(userID);
currentUserRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
studentData = dataSnapshot.getValue(studentInformation.class);
//studentID I delcared outside the onCreate. Does the 'studentID' store from Firebase?
studentFullName = studentData.getStudentFullName();//This one
textViewEmail.setText(studentData.getEmail());//Its working, display
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});
}
//At 'onCreate'
textViewStudentName.setText(studentFullName); //Can I do like this?
更新日期:2018年6月16日问题:
我想从新的FireBaseDatabaseCampus中的FireBase数据库获取值。
我做了这个,应用程序崩溃了。请帮帮我。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Inflate the layout for this fragment
View viewMain = inflater.inflate(R.layout.fragment_campus, container, false);
recyclerView = viewMain.findViewById(R.id.recycleViewCampus);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));//Ini penting nak display
firebaseAuth = FirebaseAuth.getInstance();//Ini penting
firebaseDatabase = FirebaseDatabase.getInstance();
//So retrieve data
userID = firebaseAuth.getUid();
databaseReferenceStudent = firebaseDatabase.getReference().child("student").child(userID);
databaseReferenceStudent.addValueEventListener(new ValueEventListener() { //So sini kita ambik value campus ~ UniKL MIIT
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
studentData = new studentInformation();//Ini declare
studentData = dataSnapshot.getValue(studentInformation.class);
//1. So student.getCampus is here
databaseReferenceCampus = firebaseDatabase.getReference().child("campus").child(studentData.getCampus()).child("post");
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
return viewMain;
}
//On Start
@Override
public void onStart() {
super.onStart();
FirebaseRecyclerAdapter<campus, CampusViewHolder> firebaseRecyclerAdapterCampus =
new FirebaseRecyclerAdapter<campus, CampusViewHolder>(
campus.class,
R.layout.campus_row,
CampusViewHolder.class,
databaseReferenceCampus //So the value getCampus is here?
) {
@Override
protected void populateViewHolder(CampusViewHolder viewHolder, campus model, int position) {
viewHolder.setDetails(getContext(), model.getImage(), model.getTitle(), model.getDate(), model.getShortDescription());
}
};
//setAdapter
recyclerView.setAdapter(firebaseRecyclerAdapterCampus);
}