在一个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() 这样用不正确......