我创建了一个Firebase Firestore,并通过注册页面在集合用户中插入了一些数据。一切正常,但是当我在Profile activity中取回数据时,首先我创建了一个类型为User(我创建的模型)的新变量mUser,但是如果我在“ for”之外使用该变量mUser,数据库中的文档,它为空。
能否请您看看并告诉我如何正确获取?预先谢谢你!
public class Profileactivity extends AppCompatactivity {
//Firebase
FirebaseAuth mAuth;
FirebaseFirestore db;
StorageReference mStorageRef;
Storagetask mUploadTask;
//Layout
ImageView profilePic;
TextView fullnameView;
TextView rateView;
TextView addressView;
//Vars
User mUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_nav);
Menu menu = bottomNavigationView.getMenu();
MenuItem menuItem = menu.getItem(1);
menuItem.setChecked(true);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId())
{
case R.id.nav_home:
Intent h = new Intent(Profileactivity.this,Mainactivity.class);
startactivity(h);
overridePendingTransition(R.anim.slide_in,R.anim.slide_out);
finish();
break;
case R.id.nav_profile:
Intent p = new Intent(Profileactivity.this,Profileactivity.class);
startactivity(p);
overridePendingTransition(R.anim.slide_in,R.anim.slide_out);
finish();
break;
case R.id.nav_settings:
Intent s = new Intent(Profileactivity.this,Settingsactivity.class);
startactivity(s);
overridePendingTransition(R.anim.slide_in,R.anim.slide_out);
finish();
break;
}
return false;
}
});
mAuth = FirebaseAuth.getInstance();
db = FirebaseFirestore.getInstance();
mStorageRef = FirebaseStorage.getInstance().getReference("users-images");
fullnameView = findViewById(R.id.fullnameview);
addressView = findViewById(R.id.addressview);
profilePic = findViewById(R.id.profilePicView);
rateView = findViewById(R.id.rate);
FirebaseUser user = FirebaseAuth.getInstance().getcurrentUser();
if(user != null)
{
db.collection("Users").whereEqualTo("email",user.getEmail())
.get()
.addOnCompleteListener(new OnCompleteListener<Querysnapshot>() {
@Override
public void onComplete(@NonNull Task<Querysnapshot> task) {
if(task.isSuccessful())
{
for(QueryDocumentsnapshot documentsnapshot: task.getResult()) {
String fullname = documentsnapshot.get("fullname").toString();
String address = documentsnapshot.get("address").toString();
String city = documentsnapshot.get("city").toString();
String state = documentsnapshot.get("state").toString();
String country = documentsnapshot.get("country").toString();
String phone = documentsnapshot.get("phone").toString();
String zipcode = documentsnapshot.get("zipcode").toString();
String type = documentsnapshot.get("type").toString();
String rate = documentsnapshot.get("rate").toString();
Boolean isactivated = documentsnapshot.getBoolean("isactivated");
List<Card> cards= (List<Card>) documentsnapshot.get("cards");
List<Comments> comments = (List<Comments>) documentsnapshot.get("comments");
List<Documents> documents = (List<Documents>) documentsnapshot.get("documents");
String profilePic = documentsnapshot.get("profilePic").toString();
String email = documentsnapshot.get("email").toString();
String password = documentsnapshot.get("password").toString();
mUser = new User(
fullname,address,city,state,country,phone,zipcode,type,rate,isactivated,cards,comments,documents,profilePic,email,password
);
}
}
}
});
}
fullnameView.setText(mUser.getFullname());
addressView.setText(mUser.getaddress());
}
}
此后我得到
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.****.Models.User.getFullname()' on a null object reference
at com.example.***.Profileactivity.onCreate(Profileactivity.java:161)
因为我在
之外使用了mUserfor(QueryDocumentsnapshot documentsnapshot: task.getResult()) {
...
}
如何获取数据并在“用于”之外使用数据?
谢谢!