问题描述
我有这将导致内存不足错误和美眉的手机,因为它泄漏内存FragmentPagerAdapter,没有空间了一个新的活动。平板电脑可以处理它工作正常,但不能在handsets.i曾尝试下面的code,但它不可能解决这个问题。如何清理上摧毁一个FragmentPagerAdapter?
@覆盖
公共无效destroyItem(ViewGroup中的容器,INT位置,Object对象){
super.destroyItem(集装箱,位置,对象);
尝试{
FragmentManager经理=((片段)对象).getFragmentManager();
FragmentTransaction反式= manager.beginTransaction();
trans.remove((片段)对象).commit();
}赶上(例外五){
// TODO自动生成catch块
e.printStackTrace();
}
}
这是我的日志
六月五日至12日:35:32.130:E / AndroidRuntime(1493):致命异常:主要
六月五日至12日:35:32.130:E / AndroidRuntime(1493):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.company.project / com.company.project.ProjectMainActivity}:android.view.InflateException:二进制XML文件行#156:错误充气类<&不明GT;
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.os.Handler.dispatchMessage(Handler.java:99)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.os.Looper.loop(Looper.java:130)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.app.ActivityThread.main(ActivityThread.java:3683)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在java.lang.reflect.Method.invokeNative(本机方法)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在java.lang.reflect.Method.invoke(Method.java:507)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在dalvik.system.NativeStart.main(本机方法)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):android.view.InflateException:二进制XML文件行#156:由错误造成充气类<&不明GT;
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.view.LayoutInflater.createView(LayoutInflater.java:518)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.view.LayoutInflater.inflate(LayoutInflater.java:408)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.view.LayoutInflater.inflate(LayoutInflater.java:320)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.view.LayoutInflater.inflate(LayoutInflater.java:276)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.app.Activity.setContentView(Activity.java:1657)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在com.company.project.ProjectMainActivity.onCreate(ProjectMainActivity.java:57)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):11 ...更多
六月五日至12日:35:32.130:E / AndroidRuntime(1493):因:java.lang.reflect.InvocationTargetException
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在java.lang.reflect.Constructor.constructNative(本机方法)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在java.lang.reflect.Constructor.newInstance(Constructor.java:415)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.view.LayoutInflater.createView(LayoutInflater.java:505)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):24 ...更多
六月五日至12日:35:32.130:E / AndroidRuntime(1493):java.lang.OutOfMemoryError:产生的原因位图大小超过VM预算
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.graphics.BitmapFactory.nativeDe $ C $检测板(本机方法)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.graphics.BitmapFactory.de codeStream(BitmapFactory.java:460)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.graphics.BitmapFactory.de codeResourceStream(BitmapFactory.java:336)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.content.res.Resources.loadDrawable(Resources.java:1709)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.content.res.Resources.getDrawable(Resources.java:581)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.content.res.Resources.loadDrawable(Resources.java:1694)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.content.res.TypedArray.getDrawable(TypedArray.java:601)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.widget.ImageView<&初始化GT;(ImageView.java:118)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在android.widget.ImageView<&初始化GT;(ImageView.java:108)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):在com.company.project.widget.AspectRatioImageView<&初始化GT;(AspectRatioImageView.java:17)
六月五日至12日:35:32.130:E / AndroidRuntime(1493):27 ...更多
我在做的project.in的previous修订版通过其观点做寻呼机
这已被添加到适配器。我厚我需要转换本作fragmentpageradapter。
@覆盖
公共无效destroyItem(查看收集,INT位置,对象o){
查看查看=(视图)O;
((ViewPager)集合).removeView(视图);
鉴于= NULL;
}
FragmentStatePagerAdapter可以是一些例子具有相似性这一解决方案的解决方案。 FragmentStatePagerAdapter只显示1片段,并删除其他片段,堆的大小留在minimum.If片段不交易它们之间的数据不像我的申请,我会建议使用FragmentStatePagerAdapter。对我来说,这是不是这样。我需要保留一些片段。当我去我的主要活动形式介绍活动中的所有碎片仍然留但不是所有的碎片necessary.My解决方案基于fragmentstatepageradapter解决方案。也许有一个更好的解决方案,但通过减少片段号,我保持堆,我已经解决了美眉的问题。
myPager.setOffscreenPageLimit(3); // previous为5
i have a FragmentPagerAdapter which causes outofmemory error and crush in the handsets since it leaks memory ,there is no room for a new activity. Tablets can handle it works fine but not on the handsets.i have tried the code below, but it couldn't solve it. How do i clean a FragmentPagerAdapter on destroy?
@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();
}
}
and this is my log
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
i am doing revision of a project.in the previous version its done by view pagerand this has been added to the adapter. i thick i need to convert this for fragmentpageradapter.
@Override
public void destroyItem(View collection, int position, Object o) {
View view = (View)o;
((ViewPager) collection).removeView(view);
view = null;
}
FragmentStatePagerAdapter can be solution for some examples which has similarity to this solution . FragmentStatePagerAdapter only shows 1 fragment and deletes other fragments so heap size stay at minimum.If fragments doesn't trading data between them unlike my application , i would suggest use FragmentStatePagerAdapter. For me this is not case. i need to keep some fragments. When i go to my main activity form intro activity all fragments are staying still but not all fragments are necessary.My solution has based on fragmentstatepageradapter solution. There maybe a better solution but by reducing fragment number that i keep in heap,i have solved crush problem.
myPager.setOffscreenPageLimit(3);//previous was 5
这篇关于如何清理FragmentPagerAdapter上破坏内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!