关于布局通胀的Android oom错误

时间:2016-07-27 00:27:09

标签: java android android-fragments

我有一个有4个不同屏幕的应用程序:

  • 主要活动:充当片段的框架
  • 主要片段:实际主片段布局(下面的代码)
  • 联系人列表:显示从移动设备中提取的联系人
  • 奖项列表:用户可以获得的奖励列表

除非我完全误读了错误信息,否则主要片段似乎存在问题,但我不确定原因。

我已经尝试将drawable的大小缩小到512kb,因为我已经读过android处理Bitmaps导致内存问题的方式。

我收到了以下错误消息:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smd.android.myapp/com.smd.android.myapp.MainActivity}: android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class android.widget.RelativeLayout
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                         at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                         at android.os.Looper.loop(Looper.java:148)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                      Caused by: android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class android.widget.RelativeLayout
                                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                                         at com.smd.android.myapp.MainActivityFragment.onCreateView(MainActivityFragment.java:55)
                                                                         at android.app.Fragment.performCreateView(Fragment.java:2220)
                                                                         at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
                                                                         at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
                                                                         at android.app.BackStackRecord.run(BackStackRecord.java:793)
                                                                         at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
                                                                         at android.app.FragmentController.execPendingActions(FragmentController.java:325)
                                                                         at android.app.Activity.performStart(Activity.java:6252)
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                         at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:148) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                      Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class android.widget.RelativeLayout
                                                                         at android.view.LayoutInflater.createView(LayoutInflater.java:645)
                                                                         at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
                                                                         at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694)
                                                                         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762)
                                                                         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
                                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                         at com.smd.android.myapp.MainActivityFragment.onCreateView(MainActivityFragment.java:55) 
                                                                         at android.app.Fragment.performCreateView(Fragment.java:2220) 
                                                                         at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973) 
                                                                         at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148) 
                                                                         at android.app.BackStackRecord.run(BackStackRecord.java:793) 
                                                                         at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535) 
                                                                         at android.app.FragmentController.execPendingActions(FragmentController.java:325) 
                                                                         at android.app.Activity.performStart(Activity.java:6252) 
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379) 
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                         at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:148) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                      Caused by: java.lang.reflect.InvocationTargetException
                                                                         at java.lang.reflect.Constructor.newInstance(Native Method)
                                                                         at android.view.LayoutInflater.createView(LayoutInflater.java:619)
                                                                         at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) 
                                                                         at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694) 
                                                                         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762) 
                                                                         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
                                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
                                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                         at com.smd.android.myapp.MainActivityFragment.onCreateView(MainActivityFragment.java:55) 
                                                                         at android.app.Fragment.performCreateView(Fragment.java:2220) 
                                                                         at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973) 
                                                                         at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148) 
                                                                         at android.app.BackStackRecord.run(BackStackRecord.java:793) 
                                                                         at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535) 
                                                                         at android.app.FragmentController.execPendingActions(FragmentController.java:325) 
                                                                         at android.app.Activity.performStart(Activity.java:6252) 
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379) 
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                         at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:148) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                      Caused by: java.lang.OutOfMemoryError: Failed to allocate a 150994956 byte allocation with 1044448 free bytes and 94MB until OOM
                                                                         at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                                                                         at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                                                                         at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
                                                                         at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
                                                                         at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
                                                                         at android.content.res.Resources.loadDrawableForCookie(Resources.java:2635)
                                                                         at android.content.res.Resources.loadDrawable(Resources.java:2540)
                                                                         at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
                                                                         at android.view.View.<init>(View.java:3948)
                                                                         at android.view.ViewGroup.<init>(ViewGroup.java:573)
                                                                         at android.widget.RelativeLayout.<init>(RelativeLayout.java:248)
                                                                         at android.widget.RelativeLayout.<init>(RelativeLayout.java:244)
                                                                         at android.widget.RelativeLayout.<init>(RelativeLayout.java:240)
                                                                         at java.lang.reflect.Constructor.newInstance(Native Method) 
                                                                         at android.view.LayoutInflater.createView(LayoutInflater.java:619) 
                                                                         at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) 
                                                                         at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694) 
                                                                         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762) 
                                                                         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
                                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
                                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                         at com.smd.android.myapp.MainActivityFragment.onCreateView(MainActivityFragment.java:55) 
                                                                         at android.app.Fragment.performCreateView(Fragment.java:2220) 
                                                                         at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973) 
                                                                         at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148) 
                                                                         at android.app.BackStackRecord.run(BackStackRecord.java:793) 
                                                                         at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535) 
                                                                         at android.app.FragmentController.execPendingActions(FragmentController.java:325) 
                                                                         at android.app.Activity.performStart(Activity.java:6252) 
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379) 
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                         at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:148) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

我相信它所指的布局文件是主要的活动片段:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context="com.smd.android.myapp.MainActivityFragment"
android:id="@+id/background_layout"
android:background="@drawable/background">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="2"
    android:id="@+id/relativeLayout2"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="182dp">


    <Button
        android:id="@+id/btn_award"
        android:layout_width="53dp"
        android:layout_height="100dp"
        android:layout_column="59"
        android:layout_gravity="right|center_vertical"
        android:layout_marginLeft="@dimen/fab_margin"
        android:layout_marginRight="@dimen/fab_margin"
        android:layout_marginStart="98dp"
        android:background="@drawable/awards"
        android:backgroundTint="@color/colorAccent"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

    <Button
        android:id="@+id/contactButton"
        android:layout_width="63dp"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:layout_column="56"
        android:layout_gravity="center"
        android:layout_marginStart="89dp"
        android:layout_toEndOf="@+id/btn_award"
        android:background="@drawable/contacts"
        android:textColor="@color/colorAccent"
        android:backgroundTint="@color/colorAccent" />

    <ImageView
        android:layout_width="250dp"
        android:layout_height="125dp"
        android:id="@+id/imageView"
        android:layout_gravity="center"
        android:background="@drawable/divider"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

<com.facebook.login.widget.LoginButton
    android:id="@+id/fb_login_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="138dp" />



<Button
    android:id="@+id/btn_sms"
    android:layout_width="64dp"
    android:layout_height="64dp"
    android:background="@drawable/main_button"
    android:elevation="5dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_margin="@dimen/activity_horizontal_margin"
    />

<ImageView
    android:layout_width="256dp"
    android:layout_height="wrap_content"
    android:id="@+id/imageView2"
    android:src="@drawable/x_header"
    android:layout_above="@+id/relativeLayout2"
    android:layout_centerHorizontal="true" />

MainActivityFragment:

public class MainActivityFragment extends Fragment {
Button btnSMS;
Button btnContact;
Button btnAward;
SMSHandle smsHandle;
FacebookHandle facebookHandle;
CallbackManager callbackManager;
LoginButton btnLoginFB;

private String TAG = "MainFragment";

public MainActivityFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    smsHandle = new SMSHandle();
    facebookHandle = new FacebookHandle();
    SMSHandle.getStoredContacts();
    FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
    callbackManager = CallbackManager.Factory.create();

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_main, container, false);

    btnSMS = (Button)v.findViewById(R.id.btn_sms);
    btnContact = (Button) v.findViewById(R.id.contactButton);
    btnAward = (Button) v.findViewById(R.id.btn_award);

    btnAward.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           getFragmentManager().beginTransaction().replace(R.id.activityContainer,new AwardListFragment()).addToBackStack(null).commit();
        }
    });

    btnContact.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           getFragmentManager().beginTransaction().replace(R.id.activityContainer,new ContactListFragment()).addToBackStack(null).commit();
        }
    });

    btnSMS.setOnClickListener(new View.OnClickListener(){
        public void onClick(View view) {
            //Send SMS
            smsHandle.sendToSelectedContacts(getActivity().getApplicationContext());


            ((MainActivity)getActivity()).requestSMS();
            // Post to Facebook, if logged in
            if(AccessToken.getCurrentAccessToken() != null) {
                facebookHandle.postStatus(facebookHandle.getMsg());
                Log.d(TAG,"Posted to Facebook");
            }

        }
    });

    btnLoginFB = (LoginButton) v.findViewById(R.id.fb_login_button);
    btnLoginFB.setFragment(this);

    btnLoginFB.setPublishPermissions(Arrays.asList("publish_actions"));

    btnLoginFB.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Toast.makeText(getActivity(),"Logged in successfully", Toast.LENGTH_SHORT).show();
            facebookHandle.getUserID();
        }

        @Override
        public void onCancel() {

        }

        @Override
        public void onError(FacebookException error) {
            Log.e(TAG,error.getMessage().toString());
        }
    });

    return v;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
}


}
编辑:我已经尝试了java.lang.OutOfMemoryError - BitmapFactory.decode(strPath)Android : java.lang.OutOfMemoryError:的建议但没有成功。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我在res/drawable文件夹中为每个图像使用了一个可绘制的个体。在drawable-xhdpi,drawable-xxhdpi等文件夹中有多个大小解决了这个问题。