我正在尝试检索用户在注册过程中在导航标题中保存的个人资料图像和用户名。这是我做的。最初,我尝试获取字符串当前用户,但是它显示了空指针异常,然后尝试了if语句,并将子级从当前用户ID 更改为用户< / strong>,它只解决了空指针,但没有从firebase中检索数据。
MainActivity.java
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;
import de.hdodenhof.circleimageview.CircleImageView;
public class MainActivity extends AppCompatActivity {
private NavigationView navigationView;
private DrawerLayout drawerLayout;
private RecyclerView postlist;
private Toolbar mToolbar;
private ActionBarDrawerToggle actionBarDrawerToggle;
private FirebaseAuth mAuth;
private FirebaseUser FirebaseUser;
private DatabaseReference UsersRef;
private CircleImageView NavProfileImage;
private TextView NavProfileUserName;
String currentUserID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirebaseUser mFirebaseUser = mAuth.getCurrentUser();
if (mFirebaseUser != null) {
currentUserID = mFirebaseUser.getUid();
}
mAuth = FirebaseAuth.getInstance();
mToolbar =(Toolbar) findViewById(R.id.main_page_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("Home");
drawerLayout = (DrawerLayout) findViewById(R.id.drawable_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(MainActivity.this,drawerLayout,R.string.drawer_open, R.string.drawer_close);
navigationView = (NavigationView)findViewById(R.id.navigation_view);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
View navView = navigationView.inflateHeaderView(R.layout.nav_header);
NavProfileImage = (CircleImageView)navView.findViewById(R.id.nav_profile_image);
NavProfileUserName = (TextView) navView.findViewById(R.id.nav_user_full_name);
UsersRef = FirebaseDatabase.getInstance().getReference().child("Users");
UsersRef.child(currentUserID).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
if(dataSnapshot.exists())
{ if (dataSnapshot.hasChild("fullname")){
String fullname = dataSnapshot.child("fullname").getValue().toString();
NavProfileUserName.setText(fullname);
}if (dataSnapshot.hasChild("profileimages")) {
String image = dataSnapshot.child("profileimages").getValue().toString();
Picasso.with(MainActivity.this).load(image).placeholder(R.drawable.profile).into(NavProfileImage);
}else {
Toast.makeText(MainActivity.this, "Profile name do not exists...", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item)
{ UserMenuSelector(item);
return false;
}
});
}
@Override
protected void onStart() {
super.onStart();
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser == null)
{
sendUserToLoginActivity();
}else{
CheckUserExistance();
}
}
private void CheckUserExistance()
{
final String current_user_id = mAuth.getCurrentUser().getUid();
UsersRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (!dataSnapshot.hasChild(current_user_id)){
sendUserToSetupActivity();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void sendUserToSetupActivity() {
Intent setupIntent = new Intent(MainActivity.this, SetupActivity.class);
setupIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(setupIntent);
finish();
}
private void sendUserToLoginActivity()
{
Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class);
loginIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(loginIntent);
finish();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item))
{
return true;
}
return super.onOptionsItemSelected(item);
}
private void UserMenuSelector(MenuItem item)
{
switch (item.getItemId())
{
case R.id.nav_home:
Toast.makeText(this,"Home",Toast.LENGTH_SHORT).show();
break;
case R.id.nav_post:
break;
case R.id.nav_Profile:
Toast.makeText(this,"profile",Toast.LENGTH_SHORT).show();
break;
case R.id.nav_find_friends:
Toast.makeText(this,"frns",Toast.LENGTH_SHORT).show();
break;
case R.id.nav_logout:
mAuth.signOut();
sendUserToLoginActivity();
break;
}
}
}
答案 0 :(得分:1)
我使用滑行从Firebase加载图像。会是这样的:
import com.bumptech.glide.Glide;
UserModel userModel = new UserModel;
Glide.with(YourActivity.this)
.load(userModel.getUser_image())
.into(user_image);
有关更多信息,请查看github repository。
答案 1 :(得分:0)
在注册用户时,您应该这样保存用户的信息
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
userModel.setUserid(user.getUid());
userModel.setMobile("120xxxxxxxx");
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName("Some Name")
.setPhotoUri("Phot url of the user")
.build();
user.updateProfile(profileUpdates);
然后,当您也要从用户的信息中检索图像时,...。
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
headerResult = new AccountHeaderBuilder()
.withActivity(MainActivity.this)
.withHeaderBackground(R.drawable.gradient_home_background)
.addProfiles(
new ProfileDrawerItem().withName(user.getDisplayName()).withEmail(user.getPhoneNumber()).withIcon(user.getPhotoUrl())
)
.withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
@Override
public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile) {
return false;
}
})
.build();
} else {
headerResult = new AccountHeaderBuilder()
.withActivity(MainActivity.this)
.withHeaderBackground(R.drawable.gradient_home_background)
.withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
@Override
public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile) {
return false;
}
})
.build();
}
答案 2 :(得分:0)
查看数据库结构后,我相信Users
节点内的子节点是当前UID
的{{1}},如果您要获取用户,则该子节点是firebase user
节点的子节点当前用户的详细信息,那么您的查询应该是这样
Users
答案 3 :(得分:0)
我知道,您在初始化之前确实使用过mAuth!因此它返回null。
FirebaseUser mFirebaseUser = **mAuth**.getCurrentUser();
if (mFirebaseUser != null) {
currentUserID = mFirebaseUser.getUid();
}
**mAuth = FirebaseAuth.getInstance();**
将 mAuth = FirebaseAuth.getInstance();
行移动到FirebaseUser mFirebaseUser = mAuth.getCurrentUser();
行之前