Java getContext()在片段中返回null。视图似乎是独立的
Java getContext()在片段中返回null。视图似乎是独立的,java,android,android-fragments,Java,Android,Android Fragments,Glide.with((getContext())).load(user.getImgURl()).into(image_profile) 上面的行将getContext返回为null,因此允许应用程序崩溃。 我在onDataChange方法下的一个片段中添加了这一点,如下所示 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle sav
Glide.with((getContext())).load(user.getImgURl()).into(image_profile)代码>
上面的行将getContext返回为null,因此允许应用程序崩溃。
我在onDataChange方法下的一个片段中添加了这一点,如下所示
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_profile, container, false);
image_profile=view.findViewById(R.id.profile_image);
username=view.findViewById(R.id.username);
storageReference=FirebaseStorage.getInstance().getReference("uploads");
fuser=FirebaseAuth.getInstance().getCurrentUser();
reference=FirebaseDatabase.getInstance().getReference("Users").child(fuser.getUid());
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
User user= dataSnapshot.getValue(User.class);
username.setText(user.getUsername());
if(user.getImgURl()!= null && user.getImgURl().equals("default")){
image_profile.setImageResource(R.mipmap.ic_launcher);
}else{
Glide.with((getContext())).load(user.getImgURl()).into(image_profile);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
image_profile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openImage();
}
});
return view;
}
该代码应该从firebase存储器中检索图像,并将其显示在图像配置文件中
以下是错误的日志:
java.lang.NullPointerException:
无法在尚未连接的视图或getActivity()返回null的片段上启动加载(通常在片段连接之前或片段销毁之后调用getActivity()时发生)。
感谢您的帮助。
提前感谢您。只需将支票添加到空即可
if (getContext() != null) {
Glide.with((getContext())).load(user.getImgURl()).into(image_profile);
}
并删除onDestroyView()或onDestroy()中的Firebase侦听器:
希望它会有帮助。我希望它能起作用
Glide.with(view.getContext()).load(user.getImgURl()).into(image_profile);
在片段中,对如下上下文使用getActivity():
以下代码:
Glide.with(getActivity())
.load(imageUri)
.into(imageview);
而不是使用getContext
。尝试使用getActivity
。我一直在工作。将其放入代码中,我们有:
Glide.with((getActivity())).load(user.getImgURl()).into(image_profile);
更多细节
这是活动和上下文之间的关系:
↳ android.content.Context
↳ android.content.ContextWrapper
↳ android.view.ContextThemeWrapper
↳ android.app.Activity
从上面的插图中,我们可以看到活动是一种上下文,这就是为什么它在这里是可用的
我希望这有帮助。快乐的编码。检查了互联网上的各种东西,最后这一个奏效了
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
username.setText(user.getUsername());
if (user.getImageURL().equals("default")){
image_profile.setImageResource(R.mipmap.ic_launcher);
} else {
if(getContext()!=null)
Glide.with(getActivity()).load(user.getImageURL()).into(image_profile);
}
}
要了解更多有关碎片生命周期的信息,请浏览此谢谢!destroyer方法允许上下文不为null。但是我仍然无法查看图像!有什么看法吗?行“if(getContext()!=null)”为我修正了它。好办法
Glide.with((getActivity())).load(user.getImgURl()).into(image_profile);
↳ android.content.Context
↳ android.content.ContextWrapper
↳ android.view.ContextThemeWrapper
↳ android.app.Activity
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
username.setText(user.getUsername());
if (user.getImageURL().equals("default")){
image_profile.setImageResource(R.mipmap.ic_launcher);
} else {
if(getContext()!=null)
Glide.with(getActivity()).load(user.getImageURL()).into(image_profile);
}
}