【问题标题】:android gridview crashes on Galaxy 3android gridview 在 Galaxy 3 上崩溃
【发布时间】:2012-06-12 14:54:00
【问题描述】:

好的,我想我有一个真正的问题需要改变。 我在 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    
};

}

这是我的主要调用函数:

  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:

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

/>

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

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

【问题讨论】:

  • 你能提供logcat消息吗?
  • @Muhannad 明天才有,我没有 Galaxy s3,今天也无法使用。如果我显示实际的 gridview 会有帮助吗?

标签: android gridview galaxy


【解决方案1】:

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

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

【讨论】:

  • 我在Galaxy S3遇到了一个问题,我认为是一样的,和解码位图有关,会导致OutOfMemoryError,所以如果你能尽快提供logcat错误信息
  • 这是从 Google play 获取的,并且是许多其他用户出现的问题,我认为它与 Galaxy 3 的问题 java.lang.OutOfMemoryError: bitmap size超出了 android 的 VM 预算相同。 graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:494) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:370) at android.graphics.drawable.Drawable.createFromResourceStream( Drawable.java:715) 在 android.content.res.Resources.loadDrawable(Resources.java:1720)
  • 查看上面的链接并告诉我们您是否获得任何好处
  • 我在下面发布了解决方案。非常感谢您的帮助!
【解决方案2】:

对于“为什么它会在 Galaxy 3 而不是在劣质手机上崩溃”的问题,答案是“我不知道”。但是,事实证明我一直在滥用gridview。对缩略图和整个图像使用相同的图像是一个坏主意。我所做的(这可能只是一种解决方法,但它有效)是使用 2 组图像,小的和全尺寸的 ines。这样做是这样的

设置缩略图:

private Integer[] mThumbIds = {
        R.drawable.third_world__small , R.drawable.annoyinggirl__small,
        R.drawable.asianfather__small, R.drawable.awkawespeng__small,
        R.drawable.awkpeng__small, R.drawable.blankdad__small,
        R.drawable.collegesenior__small, R.drawable.courage__small,
        R.drawable.frog__small, R.drawable.frynotsure__small,
        R.drawable.goodguygreg__small, R.drawable.scumbag__small,
        R.drawable.raptor__small,R.drawable.keano__small,
        R.drawable.successkid__small, R.drawable.wonka__small,
        R.drawable.braceyourselves__small, R.drawable.onedoesnotsimply__small,
        R.drawable.firstworldproblem__small, R.drawable.amitheonlyone__small,
        R.drawable.badluckbrian__small, R.drawable.interestingman__small,
        R.drawable.toodamnhigh__small, R.drawable.def__small    
};

public View getView(int position, View convertView, ViewGroup parent) {
    //clearAllResources();
    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;
}

然后,创建另一个数组:

private Map<Integer, Integer> mThumbIdToFullSizeId = new HashMap<Integer,Integer>();
public ImageAdapter(Context c, int width, int height) {
    mContext = c;
    mWidth = width;
    mHeight = height;
    mThumbIdToFullSizeId.put(R.drawable.third_world__small, R.drawable.third_world);
    mThumbIdToFullSizeId.put(R.drawable.annoyinggirl__small,R.drawable.annoyinggirl);
    mThumbIdToFullSizeId.put(R.drawable.asianfather__small, R.drawable.asianfather);
    mThumbIdToFullSizeId.put(R.drawable.awkawespeng__small, R.drawable.awkawespeng);
    mThumbIdToFullSizeId.put(R.drawable.awkpeng__small, R.drawable.awkpeng);
    mThumbIdToFullSizeId.put(R.drawable.blankdad__small, R.drawable.blankdad);
    mThumbIdToFullSizeId.put(R.drawable.collegesenior__small, R.drawable.collegesenior);
    mThumbIdToFullSizeId.put(R.drawable.courage__small, R.drawable.courage);
    mThumbIdToFullSizeId.put(R.drawable.frog__small, R.drawable.frog);
    mThumbIdToFullSizeId.put(R.drawable.frynotsure__small, R.drawable.frynotsure);
    mThumbIdToFullSizeId.put(R.drawable.goodguygreg__small, R.drawable.goodguygreg);
    mThumbIdToFullSizeId.put(R.drawable.scumbag__small, R.drawable.scumbag);
    mThumbIdToFullSizeId.put(R.drawable.raptor__small, R.drawable.raptor);
    mThumbIdToFullSizeId.put(R.drawable.keano__small, R.drawable.keano);
    mThumbIdToFullSizeId.put(R.drawable.successkid__small, R.drawable.successkid);
    mThumbIdToFullSizeId.put(R.drawable.wonka__small, R.drawable.wonka);
    mThumbIdToFullSizeId.put(R.drawable.braceyourselves__small, R.drawable.braceyourselves);
    mThumbIdToFullSizeId.put(R.drawable.onedoesnotsimply__small, R.drawable.onedoesnotsimply);
    mThumbIdToFullSizeId.put(R.drawable.firstworldproblem__small, R.drawable.firstworldproblem);
    mThumbIdToFullSizeId.put(R.drawable.amitheonlyone__small, R.drawable.amitheonlyone);
    mThumbIdToFullSizeId.put(R.drawable.badluckbrian__small, R.drawable.badluckbrian);
    mThumbIdToFullSizeId.put(R.drawable.interestingman__small, R.drawable.interestingman);
    mThumbIdToFullSizeId.put(R.drawable.toodamnhigh__small, R.drawable.toodamnhigh);
    mThumbIdToFullSizeId.put(R.drawable.def__small, R.drawable.def);
}

那么,对于getview函数:

public long getItemId(int position) {

    return mThumbIdToFullSizeId.get(mThumbIds[position]);
}

而且一切都会对内存更加友好。 我给 Muhannad 打勾,因为它给了我http://developer.android.com/training/displaying-bitmaps/load-bitmap.html 的链接,这对我有很大帮助

【讨论】:

  • 很好我很高兴我给了你一个小费但是你做了所有的事情,谢谢!!!
【解决方案3】:

我遇到了类似的问题。我的应用程序始终运行良好,没有任何问题,但在 Android 4 设备上开始因 OutOfMemory 错误而崩溃。尤其是在加载 GalleryView 适配器时崩溃。在阅读了许多官方文档(http://android.developer.com)的页面后,我了解到内存的使用与旧版本的 SO 不同,但最终解决了清单文件的问题。我将 Android 3.0 设置为项目构建目标,添加 android:targetSdkVersion="15",将 android:largeHeap="true" 添加到应用程序标签,最后添加支持屏幕 android:anyDensity="false"

希望这对你也有帮助!

【讨论】:

  • 谢谢!就像我在下面写的那样,我已经以不同的方式解决了这个问题,但是下次遇到这样的问题时我会记住这一点
  • 我想知道这些设置的附带影响是什么......我在加载一些位图时遇到了同样的问题,我想我会结束为 SGS3 创建一个不同的 apk 或直接将其设置为不支持的设备。我宁愿失去数百名用户,也不愿收到数百次关于崩溃的投诉。
  • 顺便说一句,我对 Android 4 本身没有任何问题,我什至有一个带 JB 的 Nexus7 和一个带 ICS 实际设备的 SGS2,我的游戏在这些设备上运行完美。所以我的问题完全出在 SGS3 上。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多