我有一个FragmentPagerAdapter导致outofmemory错误并且因为它泄漏了内存而导致手机崩溃,没有新的活动空间。平板电脑可以处理它工作正常但不在手机上。我已经尝试了下面的代码,但它无法解决它。如何清除Frag上的FragmentPagerAdapter?
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
try {
FragmentManager manager = ((Fragment)object).getFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove((Fragment)object).commit();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这是我的日志
05-12 06:35:32.130: E/AndroidRuntime(1493): FATAL EXCEPTION: main
05-12 06:35:32.130: E/AndroidRuntime(1493): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.project/com.company.project.ProjectMainActivity}: android.view.InflateException: Binary XML file line #156: Error inflating class <unknown>
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.os.Handler.dispatchMessage(Handler.java:99)
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.os.Looper.loop(Looper.java:130)
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-12 06:35:32.130: E/AndroidRuntime(1493): at java.lang.reflect.Method.invokeNative(Native Method)
05-12 06:35:32.130: E/AndroidRuntime(1493): at java.lang.reflect.Method.invoke(Method.java:507)
05-12 06:35:32.130: E/AndroidRuntime(1493): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-12 06:35:32.130: E/AndroidRuntime(1493): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-12 06:35:32.130: E/AndroidRuntime(1493): at dalvik.system.NativeStart.main(Native Method)
05-12 06:35:32.130: E/AndroidRuntime(1493): Caused by: android.view.InflateException: Binary XML file line #156: Error inflating class <unknown>
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.view.LayoutInflater.createView(LayoutInflater.java:518)
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
05-12 06:35:32.130: E/AndroidRuntime(1493): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.app.Activity.setContentView(Activity.java:1657)
05-12 06:35:32.130: E/AndroidRuntime(1493): at com.company.project.ProjectMainActivity.onCreate(ProjectMainActivity.java:57)
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-12 06:35:32.130: E/AndroidRuntime(1493): ... 11 more
05-12 06:35:32.130: E/AndroidRuntime(1493): Caused by: java.lang.reflect.InvocationTargetException
05-12 06:35:32.130: E/AndroidRuntime(1493): at java.lang.reflect.Constructor.constructNative(Native Method)
05-12 06:35:32.130: E/AndroidRuntime(1493): at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.view.LayoutInflater.createView(LayoutInflater.java:505)
05-12 06:35:32.130: E/AndroidRuntime(1493): ... 24 more
05-12 06:35:32.130: E/AndroidRuntime(1493): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.content.res.Resources.loadDrawable(Resources.java:1709)
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.content.res.Resources.getDrawable(Resources.java:581)
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.content.res.Resources.loadDrawable(Resources.java:1694)
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.widget.ImageView.<init>(ImageView.java:118)
05-12 06:35:32.130: E/AndroidRuntime(1493): at android.widget.ImageView.<init>(ImageView.java:108)
05-12 06:35:32.130: E/AndroidRuntime(1493): at com.company.project.widget.AspectRatioImageView.<init>(AspectRatioImageView.java:17)
05-12 06:35:32.130: E/AndroidRuntime(1493): ... 27 more
我正在修改一个项目。在之前的版本中,它由view pager完成 这已被添加到适配器。我很厚,我需要转换为fragmentpageradapter。
@Override
public void destroyItem(View collection, int position, Object o) {
View view = (View)o;
((ViewPager) collection).removeView(view);
view = null;
}
答案 0 :(得分:1)
OutOfMemoryError
并不一定意味着内存泄漏。
您的堆栈跟踪显示
java.lang.OutOfMemoryError: bitmap size exceeds VM budget
这意味着您正在尝试加载位图并且内存不足。
您应该学习如何display bitmaps efficiently。
同样使用FragmentPagerAdapter
,我们通常不会覆盖destroyItem()
。
您的适配器应如下所示:
public static class MyPagerAdapter extends FragmentPagerAdapter {
private static final int COUNT = 5;
public MyPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public int getCount() {
return COUNT;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FragmentA();
case 1:
return new FragmentB();
case 2:
return new FragmentC();
case 3:
return new FragmentD();
case 4:
return new FragmentE();
default:
return null;
}
}
}
答案 1 :(得分:0)
FragmentStatePagerAdapter可以解决一些与此解决方案相似的示例。 FragmentStatePagerAdapter只显示1个片段并删除其他片段,因此堆大小保持最小。如果片段不在我们之间交换数据,那么我建议使用FragmentStatePagerAdapter。对我来说,情况并非如此。我需要保留一些碎片。当我进入我的主要活动形式介绍活动时,所有片段都保持不变但并非所有片段都是必需的。我的解决方案基于fragmentstatepageradapter解决方案。可能有一个更好的解决方案,但通过减少我保留在堆中的碎片数量,我已经解决了挤压问题。
myPager.setOffscreenPageLimit(3);//previous was 5