ANDROID Fragment中动态创建ImageView

在一个ACTIVITY (CXmain) 中包含N个Fragment.现在在其中一个Fragment中需要动态创建imageview 但是运行时却出错.
目的是在在Fragment中创建一个可以滑动显示图片的功能 利用ScrollView来实现.下面是代码:

public class CtFragment extends Fragment  implements OnClickListener {
......................
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    ...................
    layout = (LinearLayout) getActivity().findViewById(R.id.layout);

    for(int i = 0; i < 8; i++)
    {
        //  通过资源文件来获得指定一个Drawable对象
        Drawable drawable = getResources().getDrawable(R.drawable.fmj);
        ImageView imageView = new ImageView(getActivity());
        imageView.setImageDrawable(drawable);
        layout.addView(imageView);
    }

是不是getActivity() 这样用不正确......

ImageView imageView = new ImageView(getActivity());这样的用法应该可以。
滑动显示图片参考一下 Fragment兼容手机与平板实现3D画廊和下拉列表(附源码)

getActivity()确实不能这样用。

正确用法应该是:

//绑定Fragment的布局文件
 fragmentLayout = inflater.inflate(R.layout.fragment_layout, container,false);
 layout = (LinearLayout) fragmentLayout.findViewById(R.id.layout);

上面的答案有帮助吗?如果还有问题,请提出来,如果对答案满意,请顶一下,并标记为采纳答案,谢谢!