java - 无法使用 Glide 库加载图像

标签 java android android-activity android-glide

我正在尝试使用 Firebase 中的 Glide 库加载图像。图片url正确(使用Logcat检查)。我在 Activity 中使用 Glide 而不是 fragment 。我收到此错误 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);



}
} 

如果我没有使用 fragment ,为什么我会收到此错误。

另外我将如何解决这个错误。请帮助我

最佳答案

从日志来看,您的上下文可能为空。尝试像这样直接使用 Activity

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

关于java - 无法使用 Glide 库加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57001486/

相关文章:

java - Spring 4 WebSocket 应用程序

java - 使用不同的参数调用不同地点的 Swing worker

java - 安卓 ButterKnife java.lang.NoClassDefFoundError : Failed resolution of: Lbutterknife/ButterKnife

android - 带有Android Studio的android-maps-utils文件夹

Android:onActivityResult() 没有调用 startActivityForResult

android - 如果我设置 Activity 主题,那么该 Activity 的弹出窗口不应该改变,这可能吗?

在 Activity 之间传递时 Android Bundle 损坏了吗?

java - ANTLR 输出及其顺序

java - java spring项目中gremlin客户端集群的最佳实践是什么

android - Android 上的可写和可执行位置