【发布时间】:2014-09-04 23:27:17
【问题描述】:
我有一个片段,它假设用毕加索创建的 Imageview 填充背景。
public class Fragment1 extends SherlockFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Context c = getActivity().getApplicationContext();
View myFragmentView = inflater.inflate(R.layout.home, container, false);
TextView text = (TextView) myFragmentView.findViewById(R.id.text);
ImageView imageView = (ImageView) myFragmentView.findViewById(R.id.image);
String url = ModalScreens.byId(1).getBackgroundImagePath();
Picasso.with(c).load(url).fit().into(imageView);
return myFragmentView;
}
}
我不知道我做错了什么,但图像无法加载。
【问题讨论】:
-
“url”字符串中返回了什么?它是有效路径吗?
-
onAttach() 为您提供片段附加到的 Activity,您可以确定,至少可以合理地确定,此时您有一个有效的 Activity。创建一个字段(推荐 mHost)并在 onAttach() 中分配 mHost = activity,然后在片段中需要 Activity 上下文的地方使用 mHost。这将有助于避免一些 NPE 问题,尽管这不是您上述问题的答案。 @adavis 确定了最可能的原因。
标签: android android-fragments picasso