gpt4 book ai didi

java.lang.OutOfMemoryError : Failed to allocate 错误

转载 作者:太空宇宙 更新时间:2023-11-03 13:12:57 26 4
gpt4 key购买 nike

所以我用 viewPager 做了一个 Activity ,但是,它在 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)

这是 Activity 的类

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;
}
}

最佳答案

在你的 list 文件中试试这个:

<application
android: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

当 Activity 被销毁时也取消绑定(bind)你的 drawables.. 这样内存就可以被垃圾收集器释放

 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 : Failed to allocate 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41421158/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com