我正在尝试显示图像的网格视图。我尝试了以下代码,但我的应用程序在运行后被强制关闭。我哪里出错了?我的代码:
AndroidGridLayoutActivity:
public class AndroidGridLayoutActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gv=(GridView)findViewById(R.id.grid_view);
gv.setAdapter(new ImageAdapter(this));
}
}
ImageAdapter.java:
public class ImageAdapter extends BaseAdapter {
private Context mcontext;
public Integer[] mThumbids={R.drawable.img9,R.drawable.img10,R.drawable.img11,R.drawable.img12,R.drawable.img13,R.drawable.img14};
public ImageAdapter(Context c)
{
mcontext=c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbids.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mThumbids[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView=new ImageView(mcontext);
imageView.setImageResource(mThumbids[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70,70));
return imageView;
}
}
主.xml:
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_view"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:stretchMode="columnWidth"/>