无法使用Glide库加载图像

时间:2019-07-12 06:44:02

标签: java android android-activity android-glide

我正在尝试使用Firebase的Glide Library加载图像。图片网址正确(已使用Logcat对其进行了检查)。我在活动中使用滑行,而不是碎片。我收到此错误java.lang.NullPointerException: You cannot start a load on a not yet attached View or a Fragment where getActivity() returns null (which usually occurs when getActivity() is called before the Fragment is attached or after the Fragment is destroyed).

这是我的代码:

public class ProfileActivity extends AppCompatActivity {

FirebaseAuth auth;
DatabaseReference databaseReference;
TextView nameTextView , bioTextView;
String name2 , bio2 , uidString;
Uri imageUri , FilePathUri;
Button logOut , editProfile;
private SlidrInterface slidr;
CircleImageView profilepic;
StorageReference storageReference;
String Storage_Path = "ProfilePictures/" , downloadUrl;
Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);

    auth = FirebaseAuth.getInstance();
    databaseReference = FirebaseDatabase.getInstance().getReference();
    final FirebaseUser user = auth.getCurrentUser();
    nameTextView = (TextView) findViewById(R.id.name);
    bioTextView = (TextView) findViewById(R.id.bio);
    logOut = (Button) findViewById(R.id.logout2);
    editProfile = (Button) findViewById(R.id.editprofile);
    slidr = Slidr.attach(this);
    profilepic = (CircleImageView) findViewById(R.id.profilepic);
    storageReference = FirebaseStorage.getInstance().getReference();

    Intent intent = getIntent();
    Bundle uid = intent.getExtras();
    uidString = uid.getString("UID");
    Log.d("UID OF USER" , "UID OF USER = "+uidString);

    final Context finalContext = context;
    databaseReference.child("users").child(uidString).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            name2 = (String) dataSnapshot.child("name").getValue();
            bio2 = (String) dataSnapshot.child("bio").getValue();


            String gender = (String) dataSnapshot.child("gender").getValue();
            String email = (String) dataSnapshot.child("email").getValue();

            Log.d("TAG", "Name: " + name2);
            Log.d("TAG", "Email: " + email);
            Log.d("TAG", "Gender: " + gender);
            Log.d("TAG", "Profile Pic URI = " + imageUri);
            nameTextView.setText(name2);
            bioTextView.setText(bio2);


            storageReference.child("ProfilePictures").child(uidString+".jpg");
            String imageUrl = (String) dataSnapshot.child("profilePic").getValue();
            GlideApp.with(context).load(imageUrl).into(profilepic);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}

    });


    logOut.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SharedPreferences settings = getSharedPreferences(SignIn.PREFS_NAME, 0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("hasLoggedIn", false);
            editor.commit();
            boolean hasLoggedIn = settings.getBoolean("hasLoggedIn", false);
            Intent intent = new Intent(ProfileActivity.this, SignIn.class);
            startActivity(intent);
        }
    });

    editProfile.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(ProfileActivity.this, EditProfile.class);
            startActivity(intent);
        }
    });

    Log.d("TAG" , "NAME OUTSIDE " +name2);



}
} 

如果我不使用片段,为什么会出现此错误。

我还将如何解决此错误。请帮助我

2 个答案:

答案 0 :(得分:3)

从日志中,您的context可能为空。尝试像这样直接使用活动

GlideApp.with(ProfileActivity.this).load(imageUrl).into(profilepic);

答案 1 :(得分:1)

尝试一下:

GlideApp.with(profilepic.getContext()).load(imageUrl).into(profilepic);