Java 如何在onCreateView中使用毕加索?
我对毕加索的代码有问题。在我的fragment_主页(导航栏的片段)中是ImageView,我想将“image.com”URL地址中的图像放在这里。 毕加索的代码是这样的Java 如何在onCreateView中使用毕加索?,java,android,picasso,Java,Android,Picasso,我对毕加索的代码有问题。在我的fragment_主页(导航栏的片段)中是ImageView,我想将“image.com”URL地址中的图像放在这里。 毕加索的代码是这样的 Picasso.get().load("image.com") .resize(300,200) .centerInside() .into(photo); 我无法将其写入我的HomeFragmentActivity中,因为fin
Picasso.get().load("image.com")
.resize(300,200)
.centerInside()
.into(photo);
我无法将其写入我的HomeFragmentActivity中,因为findViewById无法“工作”,所以代码应该是这样的
HomeFragmentActivity>必须放置代码的位置
public class HomeFragment extends Fragment {
private ImageView photo;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home /*in this layout is ImageView where Picasso is inserting image from URL addres*/) , container, false);
}
}
当代码在其他地方时(例如在onCreate方法中的MainActivity中),应用程序将崩溃。请帮忙。谢谢
写下您的建议,也许毕加索代码不一定要在HomeFragmentActivity中?您可以这样声明:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
photo = view.findViewById(R.id.photo);
Picasso.with(context).load("url")
.resize(300,200)
.centerInside()
.into(photo);
return view;
}
或
更好地使用onViewCreated
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
photo = view.findViewById(R.id.photo);
Picasso.with(context).load("url")
.resize(300,200)
.centerInside()
.into(photo);
}