我有GridView的自定义适配器,我在其中显示来自服务器的数据, 我成功显示数据但是当我滚动gridview多次时它给我错误java.lang.OutOfMemoryError我的logcat错误如下:
用于显示图像我正在使用通用图像加载器
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.build();
ImageLoader.getInstance().init(config);
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ic_stub)
.showImageOnFail(R.drawable.ic_stub)
.resetViewBeforeLoading(false)
.delayBeforeLoading(1000)
.cacheInMemory(true)
.cacheOnDisc(true)
.considerExifParams(false)
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
.bitmapConfig(Bitmap.Config.RGB_565)
.imageScaleType(ImageScaleType.EXACTLY)
.displayer(new SimpleBitmapDisplayer())
.handler(new Handler())
.build();
ImageLoader.getInstance().displayImage(image_url_string,
image_coupon, options);
06-23 09:09:12.400: E/AndroidRuntime(4387): FATAL EXCEPTION: main
06-23 09:09:12.400: E/AndroidRuntime(4387): Process: com.example.coupon, PID: 4387
06-23 09:09:12.400: E/AndroidRuntime(4387): java.lang.OutOfMemoryError
06-23 09:09:12.400: E/AndroidRuntime(4387): at java.lang.reflect.Constructor.constructNative(Native Method)
06-23 09:09:12.400: E/AndroidRuntime(4387): at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
06-23 09:09:12.400: E/AndroidRuntime(4387): at android.view.LayoutInflater.createView(LayoutInflater.java:594)
06-23 09:09:12.400: E/AndroidRuntime(4387): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
06-23 09:09:12.400: E/AndroidRuntime(4387): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
06-23 09:09:12.400: E/AndroidRuntime(4387): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
06-23 09:09:12.400: E/AndroidRuntime(4387): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
06-23 09:09:12.400: E/AndroidRuntime(4387): at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
06-23 09:09:12.400: E/AndroidRuntime(4387): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
06-23 09:09:12.400: E/AndroidRuntime(4387): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
06-23 09:09:12.400: E/AndroidRuntime(4387): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
06-23 09:09:12.400: E/AndroidRuntime(4387): at com.example.coupon.adapter.CouponsAdapter.getView(CouponsAdapter.java:113)
06-23 09:09:12.400: E/AndroidRuntime(4387): at android.widget.AbsListView.obtainView(AbsListView.java:2240)
06-23 09:09:12.400: E/AndroidRuntime(4387): at android.widget.GridView.makeAndAddView(GridView.java:1345)
06-23 09:09:12.400: E/AndroidRuntime(4387): at android.widget.GridView.makeRow(GridView.java:345)
06-23 09:09:12.400: E/AndroidRuntime(4387): at android.widget.GridView.fillDown(GridView.java:287)
06-23 09:09:12.400: E/AndroidRuntime(4387): at android.widget.GridView.correctTooLow(GridView.java:667)
06-23 09:09:12.400: E/AndroidRuntime(4387): at android.widget.GridView.fillGap(GridView.java:263)
06-23 09:09:12.400: E/AndroidRuntime(4387): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5136)
06-23 09:09:12.400: E/AndroidRuntime(4387): at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4247)
06-23 09:09:12.400: E/AndroidRuntime(4387): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
06-23 09:09:12.400: E/AndroidRuntime(4387): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
06-23 09:09:12.400: E/AndroidRuntime(4387): at android.view.Choreographer.doFrame(Choreographer.java:543)
06-23 09:09:12.400: E/AndroidRuntime(4387): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
06-23 09:09:12.400: E/AndroidRuntime(4387): at android.os.Handler.handleCallback(Handler.java:733)
06-23 09:09:12.400: E/AndroidRuntime(4387): at android.os.Handler.dispatchMessage(Handler.java:95)
06-23 09:09:12.400: E/AndroidRuntime(4387): at android.os.Looper.loop(Looper.java:136)
06-23 09:09:12.400: E/AndroidRuntime(4387): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-23 09:09:12.400: E/AndroidRuntime(4387): at java.lang.reflect.Method.invokeNative(Native Method)
06-23 09:09:12.400: E/AndroidRuntime(4387): at java.lang.reflect.Method.invoke(Method.java:515)
06-23 09:09:12.400: E/AndroidRuntime(4387): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-23 09:09:12.400: E/AndroidRuntime(4387): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-23 09:09:12.400: E/AndroidRuntime(4387): at dalvik.system.NativeStart.main(Native Method)
请帮我解决这个错误。谢谢。
答案 0 :(得分:0)
在这里,我建议您在创建newinstance之前删除旧实例,因为每次创建newinstance并且之前的内存不会被旧实例释放,因此当您的设备内存在某些尝试后变低时。它会让你失去内存错误。所以只需清除以前的实例和记忆。
答案 1 :(得分:0)
您需要在创建新实例之前删除旧实例。