老是跑出这个错误来:
这个是我的xml代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="3" >
</GridView>
</RelativeLayout>
这个是我的java源文件的代码
package com.siyehuazhilian.gridviewdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class MainActivity extends Activity {
// 声明控件对象
private GridView gridView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 通过id来得到对应的控件
gridView = (GridView) findViewById(R.id.gridview);
// 为控件设置适配器
gridView.setAdapter(new ImageAdapter(this));
// 为GridView绑定监听器
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
System.out.println("子元素被点击了");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
其中自定义适配器的代码如下:
package com.siyehuazhilian.gridviewdemo;
import java.lang.reflect.Field;
import java.util.ArrayList;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
// 资源id数组
private ArrayList<Integer> imgIds = new ArrayList<Integer>();
// Context对象
private Context context;
public ImageAdapter(Context context) {
this.context = context;
// 通过反射得到资源,并把id存储到id数组中
Field[] fields = R.drawable.class.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
// 只有包含img的文件名才会被读取
if (fields[i].getName().contains("img")) {
try {
imgIds.add(fields[i].getInt(R.drawable.class));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
@Override
public int getCount() {
return imgIds.size();
}
@Override
public Object getItem(int position) {
return imgIds.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(imgIds.get(position));
return imageView;
}
}
------解决方案--------------------
内存溢出了 记得回收你的图片资源
------解决方案--------------------
图片过大了,首先回收不使用的bitmap,其次是把过大的图片质量缩小。用 BitmapFactory.Options相关参数。