我该如何解决这个java.outOfMemoryError

时间:2014-04-19 04:30:42

标签: java android

我正在尝试滚动列表活动,但它是迟滞然后崩溃,这是我的惯用代码:

public class CustomSearch extends ArrayAdapter<SearchFItem> {
    private List<SearchFItem> data;
    Context context;




    private static class ViewHolder {
        ImageView imageView1;
        TextView Name;
    }



    public CustomSearch(Context context, int textViewResourceId,
            List<SearchFItem> data) {
        super(context, textViewResourceId, data);


        this.data = data;
        this.context = context;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        SearchFItem item = data.get(position);
        ViewHolder holder = null;

        View v = convertView;
        if (v == null) {
            v = LayoutInflater.from(getContext()).inflate(
                    R.layout.friends_view, null);
            holder = new ViewHolder();
            holder.imageView1 = (ImageView) v.findViewById(R.id.proView1);
            holder.Name = (TextView) v.findViewById(R.id.nameView1);
        } else {
            holder = new ViewHolder();
            holder.imageView1 = (ImageView) v.findViewById(R.id.proView1);
            holder.Name = (TextView) v.findViewById(R.id.nameView1);
        }
        final byte[] encodeByte = Base64.decode(item.getProfPic(),
                Base64.DEFAULT);

        final Bitmap profpic = BitmapFactory.decodeByteArray(encodeByte, 0,
                encodeByte.length);
        holder.imageView1.setImageBitmap(profpic);
        final String name = item.getName();
        holder.Name.setText(name);

        return v;
    }


}

这是我的错误代码:

04-19 00:16:24.540: E/AndroidRuntime(1709): FATAL EXCEPTION: main
04-19 00:16:24.540: E/AndroidRuntime(1709): Process: com.example.whiteboard, PID: 1709
04-19 00:16:24.540: E/AndroidRuntime(1709): java.lang.OutOfMemoryError
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:500)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:523)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at com.example.whiteboard.CustomSearch.getView(CustomSearch.java:91)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.widget.AbsListView.obtainView(AbsListView.java:2240)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.widget.ListView.makeAndAddView(ListView.java:1790)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.widget.ListView.fillUp(ListView.java:725)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.widget.ListView.fillGap(ListView.java:664)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5136)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3236)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.widget.AbsListView.onTouchMove(AbsListView.java:3580)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.widget.AbsListView.onTouchEvent(AbsListView.java:3424)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.view.View.dispatchTouchEvent(View.java:7706)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2210)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.view.View.dispatchPointerEvent(View.java:7886)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3954)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3833)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5602)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5582)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5553)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5682)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.os.MessageQueue.nativePollOnce(Native Method)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.os.MessageQueue.next(MessageQueue.java:138)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.os.Looper.loop(Looper.java:123)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at java.lang.reflect.Method.invokeNative(Native Method)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at java.lang.reflect.Method.invoke(Method.java:515)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-19 00:16:24.540: E/AndroidRuntime(1709):     at dalvik.system.NativeStart.main(Native Method)

感谢您的帮助。我一直坚持这个问题。但这是在尝试加载配置文件列表。

1 个答案:

答案 0 :(得分:1)

如果您有高分辨率图像,则应根据不同设备缩小图像以加载图像,而不会遇到内存问题。

在您的情况下,某些手机在第一次运行时可能不会表现出相同的行为,但最终(这是它变得“滞后”的部分),如果不处理优化的图像加载解决方案,应用程序将崩溃。

检查“内存问题”:

将缩小版本加载到内存中的主题。
Android developer optimize bitmaps help
Out of memory error on Android

关于如何避免它们:
How to avoid an out of memory error while using bitmaps in Android

概述:
Best practices listed on a blog
More of memory leaks - overall

在为imageview设置位图之前,即:

holder.imageView1.setImageBitmap(profpic);  

获取缩放位图或尝试其他选项,例如inSampleSize ref: inSampleSize,请检查您的解决方案如何影响图像质量。
Reference: inSampleSize