java-在GridView组件中显示多个图像时,Android应用程序崩溃

java-在GridView组件中显示多个图像时,Android应用程序崩溃,第1张

概述我是这个网站的新手,我在这里是因为我在其他任何地方都找不到此答案,所以我想看看能否获得帮助!我的项目中有一个GridView以及一个图像适配器.我需要以下代码的帮助:packagecom.humanoid.sigma;importandroid.app.Activity;importandroid.content.Context;importandroid.v

我是这个网站的新手,我在这里是因为我在其他任何地方都找不到此答案,所以我想看看能否获得帮助!

我的项目中有一个GrIDVIEw以及一个图像适配器.我需要以下代码的帮助:

package com.humanoID.sigma;import androID.app.Activity;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 {private Context mContext;public Integer[] Tattoos = {        R.drawable.tattoo1, R.drawable.tattoo2,        R.drawable.tattoo3, R.drawable.tattoo4,        R.drawable.tattoo5, /*R.drawable.tattoo6,        R.drawable.tattoo7, R.drawable.tattoo8,        R.drawable.tattoo9, R.drawable.tattoo10,         R.drawable.tattoo11, R.drawable.tattoo12,        R.drawable.tattoo13, R.drawable.tattoo14,        R.drawable.tattoo15, R.drawable.tattoo16,        R.drawable.tattoo17, R.drawable.tattoo18,        R.drawable.tattoo19, R.drawable.tattoo20,         R.drawable.tattoo21, R.drawable.tattoo22,        R.drawable.tattoo23, R.drawable.tattoo24,        R.drawable.tattoo25, R.drawable.tattoo26,        R.drawable.tattoo27, R.drawable.tattoo28,        R.drawable.tattoo29, R.drawable.tattoo30,         R.drawable.tattoo31, R.drawable.tattoo32,        R.drawable.tattoo33, R.drawable.tattoo34,        R.drawable.tattoo35, R.drawable.tattoo36,        R.drawable.tattoo37, R.drawable.tattoo38,        R.drawable.tattoo39, R.drawable.tattoo40,         R.drawable.tattoo41, R.drawable.tattoo42,        R.drawable.tattoo43, R.drawable.tattoo44,        R.drawable.tattoo45, R.drawable.tattoo46,        R.drawable.tattoo47, R.drawable.tattoo48,        R.drawable.tattoo49, R.drawable.tattoo50,         R.drawable.tattoo51, R.drawable.tattoo52,        R.drawable.tattoo53, R.drawable.tattoo54,        R.drawable.tattoo55, R.drawable.tattoo56,        R.drawable.tattoo57, R.drawable.tattoo58,        R.drawable.tattoo59, R.drawable.tattoo60,         R.drawable.tattoo61, R.drawable.tattoo62,        R.drawable.tattoo63, R.drawable.tattoo64,        R.drawable.tattoo65, R.drawable.tattoo66,        R.drawable.tattoo67, R.drawable.tattoo68,        R.drawable.tattoo69, R.drawable.tattoo70,         R.drawable.tattoo71, R.drawable.tattoo72,        R.drawable.tattoo73, R.drawable.tattoo74,        R.drawable.tattoo75, R.drawable.tattoo76,        R.drawable.tattoo77, R.drawable.tattoo78,        R.drawable.tattoo79, R.drawable.tattoo80,        R.drawable.tattoo81, R.drawable.tattoo82,*/};// Constructorpublic ImageAdapter(Context c){    mContext = c;}public int getCount() {    return Tattoos.length;}public Object getItem(int position) {    return Tattoos[position];}public long getItemID(int position) {    return 0;}@OverrIDepublic VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) {    ImageVIEw imageVIEw = new ImageVIEw(mContext);    imageVIEw.setimageResource(Tattoos[position]);    imageVIEw.setScaleType(ImageVIEw.ScaleType.CENTER_CROP);    imageVIEw.setLayoutParams(new GrIDVIEw.LayoutParams(100, 70));    return imageVIEw;}}

Logcat堆栈跟踪:

06-28 17:51:14.104: E/AndroIDRuntime(818): FATAL EXCEPTION: main 06-28
17:51:14.104: E/AndroIDRuntime(818):
java.lang.OutOfMemoryError 06-28 17:51:14.104: E/AndroIDRuntime(818):
at
androID.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 06-28
17:51:14.104: E/AndroIDRuntime(818): at
androID.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
06-28 17:51:14.104: E/AndroIDRuntime(818): at
androID.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
06-28 17:51:14.104: E/AndroIDRuntime(818): at
androID.graphics.drawable.Drawable.createFromresourceStream(Drawable.java:785)
06-28 17:51:14.104: E/AndroIDRuntime(818): at
androID.content.res.Resources.loadDrawable(Resources.java:1965) 06-28
17:51:14.104: E/AndroIDRuntime(818): at
androID.content.res.Resources.getDrawable(Resources.java:660) 06-28
17:51:14.104: E/AndroIDRuntime(818): at
androID.Widget.ImageVIEw.resolveUri(ImageVIEw.java:616) 06-28
17:51:14.104: E/AndroIDRuntime(818): at
androID.Widget.ImageVIEw.setimageResource(ImageVIEw.java:349) 06-28
17:51:14.104: E/AndroIDRuntime(818): at
com.humanoID.sigma.ImageAdapter.getVIEw(ImageAdapter.java:80) 06-28
17:51:14.104: E/AndroIDRuntime(818): at
androID.Widget.AbsListVIEw.obtainVIEw(AbsListVIEw.java:2143) 06-28
17:51:14.104: E/AndroIDRuntime(818): at
androID.Widget.GrIDVIEw.makeAndAddVIEw(GrIDVIEw.java:1341) 06-28
17:51:14.104: E/AndroIDRuntime(818): at
androID.Widget.GrIDVIEw.makeRow(GrIDVIEw.java:341) 06-28 17:51:14.104:
E/AndroIDRuntime(818): at
androID.Widget.GrIDVIEw.fillDown(GrIDVIEw.java:283) 06-28
17:51:14.104: E/AndroIDRuntime(818): at
androID.Widget.GrIDVIEw.fillFromtop(GrIDVIEw.java:417) 06-28
17:51:14.104: E/AndroIDRuntime(818): at
androID.Widget.GrIDVIEw.layoutChildren(GrIDVIEw.java:1229) 06-28
17:51:14.104: E/AndroIDRuntime(818): at
androID.Widget.AbsListVIEw.onLayout(AbsListVIEw.java:1994) 06-28
17:51:14.104: E/AndroIDRuntime(818): at
androID.vIEw.VIEw.layout(VIEw.java:14003) 06-28 17:51:14.104:
E/AndroIDRuntime(818): at
androID.vIEw.VIEwGroup.layout(VIEwGroup.java:4375) 06-28 17:51:14.104:
E/AndroIDRuntime(818): at
androID.Widget.relativeLayout.onLayout(relativeLayout.java:985) 06-28
17:51:14.104: E/AndroIDRuntime(818): at
androID.vIEw.VIEw.layout(VIEw.java:14003) 06-28 17:51:14.104:
E/AndroIDRuntime(818): at
androID.vIEw.VIEwGroup.layout(VIEwGroup.java:4375) 06-28 17:51:14.104:
E/AndroIDRuntime(818): at
androID.Widget.FrameLayout.onLayout(FrameLayout.java:448) 06-28
17:51:14.104: E/AndroIDRuntime(818): at
androID.vIEw.VIEw.layout(VIEw.java:14003) 06-28 17:51:14.104:
E/AndroIDRuntime(818): at
androID.vIEw.VIEwGroup.layout(VIEwGroup.java:4375) 06-28 17:51:14.104:
E/AndroIDRuntime(818): at
androID.Widget.linearLayout.setChildFrame(linearLayout.java:1663)
06-28 17:51:14.104: E/AndroIDRuntime(818): at
androID.Widget.linearLayout.layoutVertical(linearLayout.java:1521)
06-28 17:51:14.104: E/AndroIDRuntime(818): at
androID.Widget.linearLayout.onLayout(linearLayout.java:1434) 06-28
17:51:14.104: E/AndroIDRuntime(818): at
androID.vIEw.VIEw.layout(VIEw.java:14003) 06-28 17:51:14.104:
E/AndroIDRuntime(818): at
androID.vIEw.VIEwGroup.layout(VIEwGroup.java:4375) 06-28 17:51:14.104:
E/AndroIDRuntime(818): at
androID.Widget.FrameLayout.onLayout(FrameLayout.java:448) 06-28
17:51:14.104: E/AndroIDRuntime(818): at
androID.vIEw.VIEw.layout(VIEw.java:14003) 06-28 17:51:14.104:
E/AndroIDRuntime(818): at
androID.vIEw.VIEwGroup.layout(VIEwGroup.java:4375) 06-28 17:51:14.104:
E/AndroIDRuntime(818): at
androID.vIEw.VIEwRootImpl.performlayout(VIEwRootImpl.java:1892) 06-28
17:51:14.104: E/AndroIDRuntime(818): at
androID.vIEw.VIEwRootImpl.performTraversals(VIEwRootImpl.java:1711)
06-28 17:51:14.104: E/AndroIDRuntime(818): at
androID.vIEw.VIEwRootImpl.doTraversal(VIEwRootImpl.java:989) 06-28
17:51:14.104: E/AndroIDRuntime(818): at
androID.vIEw.VIEwRootImpl$TraversalRunnable.run(VIEwRootImpl.java:4351)
06-28 17:51:14.104: E/AndroIDRuntime(818): at
androID.vIEw.Choreographer$CallbackRecord.run(Choreographer.java:749)
06-28 17:51:14.104: E/AndroIDRuntime(818): at
androID.vIEw.Choreographer.doCallbacks(Choreographer.java:562) 06-28
17:51:14.104: E/AndroIDRuntime(818): at
androID.vIEw.Choreographer.doFrame(Choreographer.java:532) 06-28
17:51:14.104: E/AndroIDRuntime(818): at
androID.vIEw.Choreographer$FramedisplayEventReceiver.run(Choreographer.java:735)
06-28 17:51:14.104: E/AndroIDRuntime(818): at
androID.os.Handler.handleCallback(Handler.java:725) 06-28
17:51:14.104: E/AndroIDRuntime(818): at
androID.os.Handler.dispatchMessage(Handler.java:92) 06-28
17:51:14.104: E/AndroIDRuntime(818): at
androID.os.Looper.loop(Looper.java:137) 06-28 17:51:14.104:
E/AndroIDRuntime(818): at
androID.app.ActivityThread.main(ActivityThread.java:5039) 06-28
17:51:14.104: E/AndroIDRuntime(818): at
java.lang.reflect.Method.invokeNative(Native Method) 06-28
17:51:14.104: E/AndroIDRuntime(818): at
java.lang.reflect.Method.invoke(Method.java:511) 06-28 17:51:14.104:
E/AndroIDRuntime(818): at
com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-28 17:51:14.104: E/AndroIDRuntime(818): at
com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:560) 06-28
17:51:14.104: E/AndroIDRuntime(818): at
dalvik.system.NativeStart.main(Native Method)

gallery.Java代码:

package com.humanoID.sigma;import androID.app.Activity;import androID.content.Context;import androID.content.Intent;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.AdapterVIEw;import androID.Widget.AdapterVIEw.OnItemClickListener;import androID.Widget.BaseAdapter;import androID.Widget.GrIDVIEw;import androID.Widget.ImageVIEw;public class gallery extends Activity {@OverrIDepublic voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.gallery);    GrIDVIEw grIDVIEw = (GrIDVIEw) findVIEwByID(R.ID.photos);    // Instance of ImageAdapter Class    grIDVIEw.setAdapter(new ImageAdapter(this));    grIDVIEw.setonItemClickListener(new OnItemClickListener() {        @OverrIDe        public voID onItemClick(AdapterVIEw<?> parent, VIEw v,                int position, long ID) {            // Sending image ID to FullScreenActivity            Intent i = new Intent(getApplicationContext(), FullScreen.class);            // passing array index            i.putExtra("ID", position);            startActivity(i);        }    });}}

从上面可以看到,我已注释掉对我不起作用的内容.我不知道这是否是因为此GrIDVIEw的图片太多,但是很烦人.一旦我注释掉这些图像,程序就可以正常运行,但是如果我删除/ *并运行该程序并选择gallery,它将仅显示黑屏.不久之后它将崩溃.

请通过我不断搜索的这段代码来帮助我,但找不到任何帮助.

解决方法:

您的问题是由于分配可绘制资源时堆大小已用尽(这是一个常见问题,因为这种 *** 作需要大量内存).

当我尝试过您的代码时,出现java.lang.OutOfMemoryError:位图大小超出VM预算.这是一个简单明了的错误信息.

然后,我进行了一些研究,并在AndroID Dev官方网站上遇到了“本地”线程:http://developer.android.com/training/displaying-bitmaps/index.html有关位图以及有关如何在应用程序中进行有效资源分配的示例.

所以我尝试了一下,并与您的代码结合在一起,这就是我得到的:

ImageAdapter.java:

package com.humanoID.sigma;import androID.app.Activity;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 {private Context mContext;public Integer[] Tattoos = {        R.drawable.tattoo1, R.drawable.tattoo2,        R.drawable.tattoo3, R.drawable.tattoo4,        R.drawable.tattoo5, R.drawable.tattoo6,        R.drawable.tattoo7, R.drawable.tattoo8,        R.drawable.tattoo9, R.drawable.tattoo10,         R.drawable.tattoo11, R.drawable.tattoo12,        R.drawable.tattoo13, R.drawable.tattoo14,        R.drawable.tattoo15, R.drawable.tattoo16,        R.drawable.tattoo17, R.drawable.tattoo18,        R.drawable.tattoo19, R.drawable.tattoo20,         R.drawable.tattoo21, R.drawable.tattoo22,        R.drawable.tattoo23, R.drawable.tattoo24,        R.drawable.tattoo25, R.drawable.tattoo26,        R.drawable.tattoo27, R.drawable.tattoo28,        R.drawable.tattoo29, R.drawable.tattoo30,         R.drawable.tattoo31, R.drawable.tattoo32,        R.drawable.tattoo33, R.drawable.tattoo34,        R.drawable.tattoo35, R.drawable.tattoo36,        R.drawable.tattoo37, R.drawable.tattoo38,        R.drawable.tattoo39, R.drawable.tattoo40,         R.drawable.tattoo41, R.drawable.tattoo42,        R.drawable.tattoo43, R.drawable.tattoo44,        R.drawable.tattoo45, R.drawable.tattoo46,        R.drawable.tattoo47, R.drawable.tattoo48,        R.drawable.tattoo49, R.drawable.tattoo50,         R.drawable.tattoo51, R.drawable.tattoo52,        R.drawable.tattoo53, R.drawable.tattoo54,        R.drawable.tattoo55, R.drawable.tattoo56,        R.drawable.tattoo57, R.drawable.tattoo58,        R.drawable.tattoo59, R.drawable.tattoo60,         R.drawable.tattoo61, R.drawable.tattoo62,        R.drawable.tattoo63, R.drawable.tattoo64,        R.drawable.tattoo65, R.drawable.tattoo66,        R.drawable.tattoo67, R.drawable.tattoo68,        R.drawable.tattoo69, R.drawable.tattoo70,         R.drawable.tattoo71, R.drawable.tattoo72,        R.drawable.tattoo73, R.drawable.tattoo74,        R.drawable.tattoo75, R.drawable.tattoo76,        R.drawable.tattoo77, R.drawable.tattoo78,        R.drawable.tattoo79, R.drawable.tattoo80,        R.drawable.tattoo81, R.drawable.tattoo82};// Constructorpublic ImageAdapter(Context c){    mContext = c;}public int getCount() {    return Tattoos.length;}public Object getItem(int position) {    return Tattoos[position];}public long getItemID(int position) {    return 0;}public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) {    //This actually is a bad solution, because every time convertVIEw is reused, you will still initialize new ImageVIEw, which is wrong    //ImageVIEw imageVIEw = new ImageVIEw(this.mContext);    //new BitmapWorkerTask(imageVIEw).execute(Tattoos[position]);    //return imageVIEw;    //Better solution    ImageVIEw imageVIEw = null;    if (convertVIEw == null) {        imageVIEw = new ImageVIEw(this.mContext);        new BitmapWorkerTask(imageVIEw).execute(Tattoos[position]);        //create new ImageVIEw if it is not present and populate it with some image    } else {        imageVIEw = (ImageVIEw) convertVIEw;        //re-use ImageVIEw that already exists in memory    }return imageVIEw;}class BitmapWorkerTask extends AsyncTask<Integer, VoID, Bitmap> {        private final WeakReference<ImageVIEw> imageVIEwReference;        private int data = 0;        public BitmapWorkerTask(ImageVIEw imageVIEw) {            // Use a WeakReference to ensure the ImageVIEw can be garbage collected            imageVIEwReference = new WeakReference<ImageVIEw>(imageVIEw);        }        // Decode image in background.        @OverrIDe        protected Bitmap doInBackground(Integer... params) {            data = params[0];            return decodeSampledBitmapFromresource(ImageAdapter.this.mContext.getResources(), data, 100, 100);        }        // Once complete, see if ImageVIEw is still around and set bitmap.        @OverrIDe        protected voID onPostExecute(Bitmap bitmap) {            if (imageVIEwReference != null && bitmap != null) {                final ImageVIEw imageVIEw = imageVIEwReference.get();                if (imageVIEw != null) {                    imageVIEw.setimageBitmap(bitmap);                    imageVIEw.setScaleType(ImageVIEw.ScaleType.CENTER_CROP);                    imageVIEw.setLayoutParams(new GrIDVIEw.LayoutParams(100, 70));                }            }        }    }    public static Bitmap decodeSampledBitmapFromresource(Resources res, int resID,                                                         int reqWIDth, int reqHeight) {        // First decode with inJustDecodeBounds=true to check dimensions        final BitmapFactory.Options options = new BitmapFactory.Options();        options.inJustDecodeBounds = true;        BitmapFactory.decodeResource(res, resID, options);        // Calculate inSampleSize        options.inSampleSize = calculateInSampleSize(options, reqWIDth, reqHeight);        // Decode bitmap with inSampleSize set        options.inJustDecodeBounds = false;        return BitmapFactory.decodeResource(res, resID, options);    }    public static int calculateInSampleSize(            BitmapFactory.Options options, int reqWIDth, int reqHeight) {        // Raw height and wIDth of image        final int height = options.outHeight;        final int wIDth = options.outWIDth;        int inSampleSize = 1;        if (height > reqHeight || wIDth > reqWIDth) {            // Calculate ratios of height and wIDth to requested height and wIDth            final int heightRatio = Math.round((float) height / (float) reqHeight);            final int wIDthRatio = Math.round((float) wIDth / (float) reqWIDth);            // Choose the smallest ratio as inSampleSize value, this will guarantee            // a final image with both dimensions larger than or equal to the            // requested height and wIDth.            inSampleSize = heightRatio < wIDthRatio ? heightRatio : wIDthRatio;        }        return inSampleSize;    }}

异步加载图像并对加载的资源执行其他 *** 作.

此解决方案对我来说效果很好,并且在您运行代码时,您实际上可以一次看到加载的图像,从重量较小的图像开始,然后向上加载,因为每个ImageVIEw都会有单独的工作程序线程,它将加载和转换资源,然后将其应用于ImageVIEw.

证明:

总结

以上是内存溢出为你收集整理的java-在GridView组件中显示多个图像时,Android应用程序崩溃全部内容,希望文章能够帮你解决java-在GridView组件中显示多个图像时,Android应用程序崩溃所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1074438.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存