无法使用 Glide 库加载图像

我正在尝试使用来自 firebase 的 Glide 库加载图像。图片 url 正确(使用 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() {


        }

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


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


呼啦一阵风
浏览 175回答 2
2回答

红糖糍粑

从日志来看,你的可能context是空的。尝试像这样直接使用活动GlideApp.with(ProfileActivity.this).load(imageUrl).into(profilepic);

ibeautiful

尝试这个 :GlideApp.with(profilepic.getContext()).load(imageUrl).into(profilepic);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java