我试图使用volley libs下载片段中的图像。但我无法修复错误:
public static class ViewFragment extends Fragment {
ImageLoader mImageLoader;
NetworkImageView mNetworkImageView;
private static String IMAGE_URL =
"http://meteo.profi-net.sk/webcams/BIELAPUT-l.jpg";
public ViewFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the NetworkImageView that will display the image.
mNetworkImageView = (NetworkImageView) getView().findViewById(R.id.networkImageView);
// Get the ImageLoader through your singleton class.
mImageLoader = VolleySingleton.getInstance(this).getImageLoader(); //!!!!!! HERE IS THE ERROR
// Set the URL of the image that should be loaded into this view, and
// specify the ImageLoader that will be used to make the request.
mNetworkImageView.setImageUrl(IMAGE_URL, mImageLoader);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_camera_view, container, false);
}
}

getInstance(this)标记为错误,但我不知道如何更改它。如果我在某些活动中编写完全相同的代码,那就更难了。
有人可以解释一下如何在片段中使用NetworkImageView吗?
答案 0 :(得分:0)
getInstance需要一个Context作为参数,Fragment不是,而Activity是。 片段可以通过调用getActivity方法来访问其保持活动。您应该在每次调用之前检查该片段是否仍附加到其活动,否则您将崩溃
所以你可以这样做:
if(isAdded())
{
.......... your code ..............
// Get the ImageLoader through your singleton class.
mImageLoader = VolleySingleton.getInstance(getActivity()).getImageLoader();
.......... your code ..............
}