使用片段时方向更改的内存不足错误

时间:2013-11-29 12:28:50

标签: android android-fragments bitmap

我有一个片段,其方向改变的布局不同。但是因为我不希望我的活动重新创建我已经使用过 android:configChanges="orientation|screenSize|keyboardHidden" 在Fragment活动中保存我的片段。

因此,在片段中,我将重写onConfiguratinoChanged,如下所示:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
    // need to change the according port/landscape layout file as recreation of activity is restricted.
    main.removeAllViews();        
    main.addView(View.inflate(getActivity(), R.layout.fragment_home, null));
}

fragment_home根视图中,我设置了纵向和横向布局不同的背景图像。

我只在布局xml中设置背景图片。

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/tablet_home_main"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@drawable/bg_landscape" //bg_portrait changed in portrait layout
  android:orientation="vertical"
  android:paddingTop="10dp" >

我将背景图片保存在drawable-hdpi文件夹中。

这是我的片段:

 public class HomeFragment extends SherlockFragment {
    LinearLayout main;
    @Override
    public View onCreateView(LayoutInflater inflater, 
           ViewGroup container,Bundle   savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_home, container,false);
    main = (LinearLayout) rootView.findViewById(R.id.tablet_home_main);     
    return rootView;
     }
    @Override
     public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    }
    @Override
        public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);        
    main.removeAllViews();
    main.addView(View.inflate(getActivity(), R.layout.fragment_home, null));
    }
 }

我的问题是当我连续更改主页片段的方向4-5次我的应用程序崩溃并显示以下错误消息时:

Caused by: java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
at android.content.res.Resources.loadDrawable(Resources.java:1965)
at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
at android.widget.ImageView.<init>(ImageView.java:120)
at android.widget.ImageView.<init>(ImageView.java:110)

1 个答案:

答案 0 :(得分:3)

首先,在

 onCreateView(...)

您正在使用布局ID R.layout.fragment_home来扩充视图,并且您将此值分配给main,如

View rootView = inflater.inflate(R.layout.fragment_home, container,false);
main = (LinearLayout) rootView.findViewById(R.id.tablet_home_main); 

所以这里主要是LinearLayout

并在

  onConfigurartiion(...)

您再次使用相同的ID R.layout.fragment_home对视图进行充气并添加到main视图。

所以你继续将LinearLayout添加到LinearLayout ......

试试这个..

    private FrameLayout frameLayout;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        frameLayout = new FrameLayout(getActivity());
        View view = inflater.inflate(R.layout.activity_main, null);
        frameLayout.addView(view);
        return frameLayout;
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        View view = frameLayout.getChildAt(0);
        Drawable drawable = view.getBackground();
        frameLayout.removeAllViews();
        if(drawable != null && drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            bitmapDrawable.getBitmap().recycle();
        }
        LayoutInflater inflater = getActivity().getLayoutInflater();
        if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            view = inflater.inflate(R.layout.landscape_layout, null);
        } else {
            view = inflater.inflate(R.layout.portraitlayout, null);
        }
        frameLayout.addView(view);
    }