android gridview在Galaxy 3上崩溃了

n00*_*mer 7 android gridview galaxy

好吧,我想我有一个真正的改变问题.我在Android中实现了gridView,按照Android开发者页面中的说明逐步完成,http://developer.android.com/resources/tutorials/views/hello-gridview.html 我更改了它,以便在单击视图时,将返回位图.这一切都适用于高级手机,如Galxy Note和Galaxy S2,以及不太先进的手机,如Galaxy ace,甚至是2年前的一些糟糕的htc.但出于某种原因,由于OutOfMemory问题,它因ICS而在Galaxy 3上崩溃.当在gridview上使用大约一半的图像时,它确实有效(尽管有点慢).这对任何人都有意义吗?

这是我对ImageAdapter的实现:

public class ImageAdapter extends BaseAdapter {
private Context mContext;
private int mWidth;
private int mHeight;

public ImageAdapter(Context c, int width, int height) {
    mContext = c;
    mWidth = width;
    mHeight = height;
}

public int getCount() {
    return mThumbIds.length;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return mThumbIds[position];
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(mWidth, mHeight));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

// references to our images
private Integer[] mThumbIds = {
        R.drawable.third_world , R.drawable.annoyinggirl,
        R.drawable.asianfather, R.drawable.awkawespeng,
        R.drawable.awkpeng, R.drawable.blankdad,
        R.drawable.collegesenior, R.drawable.courage,
        R.drawable.frog, R.drawable.frynotsure,
        R.drawable.goodguygreg, R.drawable.scumbag,
        R.drawable.raptor,R.drawable.keano,
        R.drawable.successkid, R.drawable.wonka,
        R.drawable.braceyourselves, R.drawable.onedoesnotsimply,
        R.drawable.firstworldproblem, R.drawable.amitheonlyone,
        R.drawable.badluckbrian, R.drawable.interestingman,
        R.drawable.toodamnhigh, R.drawable.def    
};
Run Code Online (Sandbox Code Playgroud)

}

这是我主要的调用函数:

  private void changeToTemplateView(){
    setContentView(R.layout.templates);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(context, (int)(dstWidth * 1.4), (int)(dstHeight * 1.4)));

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

            Options opts = new Options();
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), (int) parent.getItemIdAtPosition(position), opts);

            //do stuff with bitmap          
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

这是gridview的xml:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/gridview"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
Run Code Online (Sandbox Code Playgroud)

/>

如果有人知道我在这里做错了什么,我将永远感激不尽

编辑:在崩溃之前,我在日志中收到错误:"FimgApiStretch:stretch failed".此外,gridview中的滚动不起作用.这是无论如何即使没有足够的缩略图导致滚动,应用程序仍然会崩溃

Muh*_*iri 2

尝试用 match_parent 替换 fill_parent 因为根据我的信息,属性值 fill_parent 在 android 2.2 及更高版本上已弃用希望这会有所帮助

你可以看到这个链接http://developer.android.com/training/displaying-bitmaps/load-bitmap.html 我希望这对你有帮助