-3
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.myfragment, container,false);
    ImageView tv = (ImageView ) v.findViewById(R.id.imageView);
    tv.setImageResource(R.drawable.h1);
    return tv;
}

为什么我会收到运行时错误?

4

1 回答 1

0

将上面的代码更改如下:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.myfragment, container,false);
    ImageView tv = (ImageView ) v.findViewById(R.id.imageView);
    tv.setImageResource(R.drawable.h1);
    return v;
}

你得到 RuntimeError 的原因是你正在返回ImageView(tv) ,onCreateView它是 view for Fragment。但是ImageViewmyfragment布局的子视图,因此无法设置它ViewFragment因此您会遇到运行时错误。

于 2013-01-16T14:26:26.223 回答