如何解决尝试设置Image时,Android ImageView空对象
我很难用来自Firebase存储数据库的用户对象中的字符串下载URL设置ImageView。 setimage()方法说Imageview是一个空对象,当我尝试调试它时,它是正确的,它显示为空。我该如何解决。我认为这与匿名内部类有关。
public class DashBoard extends AppCompatActivity {
private FirebaseAuth mAuth;
private ArrayList<String> motivatingMessages;
private Button gymLocations,profile,health;
public ImageView profileImageView;
private TextView welcome;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
showProfilePicture();
profileImageView = findViewById(R.id.personalProfile);
mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
setContentView(R.layout.activity_dash_board);
//getSupportActionBar().setTitle("Welcome To Bodify");
profile = findViewById(R.id.buttonProfile);
gymLocations = findViewById(R.id.gymFinderButton);
health = findViewById(R.id.healthButton);
welcome = findViewById(R.id.welcomeUser);
final String userID = mAuth.getUid();
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("User").child(userID);
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
User user = snapshot.getValue(User.class);
if (user != null) {
welcome.setText("User Logged in: ");
welcome.append(user.getUserName());
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(DashBoard.this,"Error Occurred: " + error.getMessage(),Toast.LENGTH_SHORT).show();
}
});
gymLocations.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),GymsNearMe.class));
}
});
profile.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),PersonalProfile.class));
}
});
health.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(DashBoard.this,Health.class));
}
});
}
public void showProfilePicture() {
mAuth = FirebaseAuth.getInstance();
final String userID = mAuth.getUid();
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("User").child(userID);
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
User user = snapshot.getValue(User.class);
String image = user.getmImageUrl();
setimage(image);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(DashBoard.this,Toast.LENGTH_SHORT).show();
}
});
}
public void setimage(String image) {
Picasso.get().load(image).into(profileImageView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu,menu);
return true;
}
@Override
public boolean onoptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.logout) {
mAuth.signOut();
startActivity(new Intent(getApplicationContext(),LogIn.class));
}
return true;
}
}
enter code here
解决方法
您似乎在扩大版面之前试图找到视图:
profileImageView = findViewById(R.id.personalProfile);
在通货膨胀之后移动那条线,所以在那条线之后:
setContentView(R.layout.activity_dash_board);
换句话说,行的顺序应为:
setContentView(R.layout.activity_dash_board);
mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
profileImageView = findViewById(R.id.personalProfile);
showProfilePicture();
,您无需在mAuth
showProfilePicture
,
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dash_board);
profileImageView = findViewById(R.id.personalProfile);
profile = findViewById(R.id.buttonProfile);
gymLocations = findViewById(R.id.gymFinderButton);
health = findViewById(R.id.healthButton);
welcome = findViewById(R.id.welcomeUser);
showProfilePicture();
mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
尝试此顺序
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。