android gridview 在 Galaxy 3 上崩溃 [英] android gridview crashes on Galaxy 3

查看:28
本文介绍了android gridview 在 Galaxy 3 上崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Ok, I think I have a real question for a change. I implemented a gridView in Android, following step by step the instructions in the Android Developers page, http://developer.android.com/resources/tutorials/views/hello-gridview.html I changed it so that when clicking on a view, the bitmap will be returned. This all works great on advanced phones like Galxy Note and Galaxy S2, and on less advanced ones like Galaxy ace and even some crappy htc from 2 years ago. But for some reason, it crashes on galaxy 3 with ICS due to OutOfMemory issues. When using about half of the images on the gridview, it does work (albeit a bit slowly). Does this make any sense to anyone?

这是我对 ImageAdapter 的实现:

This is my implementation of 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    
};

}

这是我的主调用函数:

  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          
        }
    });
}

这是gridview的xml:

And this is the xml for the gridview:

<?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"

/>

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

If anyone has any idea what I might be doing wrong here, I will be forever grateful

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

Before the crash, I get an error in the log: "FimgApiStretch:stretch failed". Also, the scrolling in the gridview doesn't work. This is regardless of the fact that even if there aren't enough thumbnails to cause a scroll, the app will still crash

推荐答案

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

try to replace fill_parent with match_parent because according to my information the attribute value fill_parent is deprecated on android 2.2 and above hope this would help

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

You can see this link http://developer.android.com/training/displaying-bitmaps/load-bitmap.html I hope this will help you

这篇关于android gridview 在 Galaxy 3 上崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆