java.lang.OutOfMemoryError:分配失败

java.lang.OutOfMemoryError:分配失败,第1张

概述因此,我使用viewPager进行了一个活动,好了,只要viewPager.setAdapter工作,它就会崩溃.这是崩溃日志.java.lang.OutOfMemoryError:Failedtoallocatea26488162byteallocationwith16777216freebytesand24MBuntilOOMatjava.lang.StringFactory.ne

因此,我使用vIEwPager进行了一个活动,好了,只要vIEwPager.setAdapter工作,它就会崩溃.这是崩溃日志.

java.lang.OutOfMemoryError: Failed to allocate a 26488162 byte allocation with 16777216 free bytes and 24MB until OOM                  at java.lang.StringFactory.newStringFromChars(Native Method)                  at java.lang.AbstractStringBuilder.toString(AbstractStringBuilder.java:629)                  at java.lang.StringBuffer.toString(StringBuffer.java:723)                  at java.io.StringWriter.toString(StringWriter.java:100)                  at androID.util.Log.getStackTraceString(Log.java:345)                  at com.androID.internal.os.RuntimeInit.Clog_e(RuntimeInit.java:61)                  at com.androID.internal.os.RuntimeInit.-wrap0(RuntimeInit.java)                  at com.androID.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:86)                  at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)                  at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)

这是活动的课程

public class SetupActivity extends AppCompatActivity {    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_setup);        VIEwPager setupPager = (VIEwPager) findVIEwByID(R.ID.setupVIEwpager);         SetupPagerAdapter setupPagerAdapter = new SetupPagerAdapter(getSupportFragmentManager());            setupPager.setAdapter(setupPagerAdapter);   }}

这是SetupPagerAdapter

public class SetupPagerAdapter extends FragmentPagerAdapter {    public SetupPagerAdapter(FragmentManager fm) {        super(fm);    }    @OverrIDe    public Fragment getItem(int position) {        if(position == 0){            return new SetupFragmentOne().newInstance();        }        return null;    }    @OverrIDe    public int getCount() {        return 1;    }}

这是SetupFragmentOne

public class SetupFragmentOne extends Fragment {    public SetupFragmentOne newInstance() {        SetupFragmentOne fragment = new SetupFragmentOne();        return fragment;    }    @Nullable    @OverrIDe    public VIEw onCreateVIEw(LayoutInflater inflater, @Nullable VIEwGroup container, @Nullable Bundle savedInstanceState) {        VIEw vIEw = inflater.inflate(R.layout.setup_fragment_1, container);        Typeface typeface = Typeface.createFromAsset(getContext().getAssets(),"coolvetica_rg.ttf");        TextVIEw tv = (TextVIEw) vIEw.findVIEwByID(R.ID.setupText1);        tv.setTypeface(typeface);        return vIEw;    }}

解决方法:

在清单文件中尝试一下:

<applicationandroID:allowBackup="true"androID:harDWareAccelerated="false"androID:icon="@mipmap/ic_launcher"androID:label="@string/app_name"androID:largeHeap="true"androID:supportsRtl="true"androID:theme="@style/Apptheme">

当Java虚拟机(JVM)由于缺少内存空间而无法分配对象,并且垃圾回收器无法释放某些空间时,会发生此错误.

尝试更改此行:

VIEw vIEw = inflater.inflate(R.layout.setup_fragment_1, container);

VIEw vIEw = inflater.inflate(R.layout.setup_fragment_1, container,false);

解决内存不足错误…更好的方法:

如果使用大量位图或可绘制图像…应将不同分辨率的图像放在不同的文件夹中… hdpi,xhdpi等…
看到这个:https://developer.android.com/guide/practices/screens_support.html

活动销毁时也请取消绑定可绘制对象..以便可以通过垃圾回收器释放内存

 private voID unbindDrawables(VIEw vIEw){        if (vIEw.getBackground() != null)        {                vIEw.getBackground().setCallback(null);        }        if (vIEw instanceof VIEwGroup && !(vIEw instanceof AdapterVIEw))        {                for (int i = 0; i < ((VIEwGroup) vIEw).getChildCount(); i++)                {                        unbindDrawables(((VIEwGroup) vIEw).getChildAt(i));                }                ((VIEwGroup) vIEw).removeAllVIEws();        }}

另请参见:https://androidactivity.wordpress.com/2011/09/24/solution-for-outofmemoryerror-bitmap-size-exceeds-vm-budget/

总结

以上是内存溢出为你收集整理的java.lang.OutOfMemoryError:分配失败全部内容,希望文章能够帮你解决java.lang.OutOfMemoryError:分配失败所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存