在3686416字节分配的内存不足,android.view.InflateException:二进制XML文件行#1:错误膨胀类<unknown>

Com*_*02x 1 android out-of-memory invocationtargetexception runtimeexception inflate-exception

在调用Asynctask内部之后,仅在向父RelativeLayout添加背景图像后出现多个错误(此错误仅在使用模拟器时出现,但在与实际设备一起使用时,堆栈跟踪未显示任何内容或正常):

public class UserAuthTask extends AsyncTask<String, Void, String> {
    private boolean success = false;

    @Override
    protected String doInBackground(String... path) {
        // TODO: attempt authentication against a network service.
        try {
            // Simulate network access.
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            return null;
        }

        Log.d(Constant.TAG_AUTH, path[0]);
        String apiRequestReturn = WebServiceUtil.getRequest(path[0]);
        if (apiRequestReturn.equals("")) {
            Log.d(Constant.TAG_AUTH, "WebService request is null");
            return null;
        } else {
            Log.d(Constant.TAG_AUTH, "WebService request has data");
            return apiRequestReturn;
        }
    }

    @Override
    protected void onPostExecute(String result) {
        userAuthTask = null;
        showProgress(false);

        super.onPostExecute(null);

        if (Boolean.parseBoolean(result) == true) {
            // Account exists, return true if the password matches.
            success  = true;
        }

        if (success) {
            // Start main activity if auth is successful
            Intent i = new Intent(AuthActivity.this, NextActivity.class);
            startActivity(i);

            application.shortToast(getApplicationContext().getResources().getString(R.string.auth_success));
            Log.d(Constant.TAG_AUTH, "Authentication successful");
        } else {
            application.shortToast(getApplicationContext().getResources().getString(R.string.invalid_account));
            Log.d(Constant.TAG_AUTH, getString(R.string.invalid_account));
            Log.d(Constant.TAG_AUTH, "Authentication failed");

            finish();
        }
    }

    @Override
    protected void onCancelled() {
        userAuthTask = null;
        showProgress(false);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是NextActivity:

public class NextActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_next);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.next, menu);
    return true;
}}
Run Code Online (Sandbox Code Playgroud)

并且错误的原因是android:background="@drawable/master_background"activity_next.xml中的行:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/master_background" >
Run Code Online (Sandbox Code Playgroud)

这只是一个棕色的图像,我只需要使用这个图像来使用我的设计师给出的资源(图像).我只是不明白当我删除该行时android:background="@drawable/master_background"我的代码没问题.顺便说一下这里的堆栈跟踪:

    07-15 11:51:20.616: E/AndroidRuntime(2392): FATAL EXCEPTION: main
    07-15 11:51:20.616: E/AndroidRuntime(2392): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sample/com.sample.NextActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.os.Handler.dispatchMessage(Handler.java:99)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.os.Looper.loop(Looper.java:137)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.app.ActivityThread.main(ActivityThread.java:5041)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at java.lang.reflect.Method.invokeNative(Native Method)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at java.lang.reflect.Method.invoke(Method.java:511)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at dalvik.system.NativeStart.main(Native Method)
    07-15 11:51:20.616: E/AndroidRuntime(2392): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.view.LayoutInflater.createView(LayoutInflater.java:613)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.app.Activity.setContentView(Activity.java:1881)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at com.sample.NextActivity.onCreate(NextActivity.java:12)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.app.Activity.performCreate(Activity.java:5104)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.app.Instrumentation.callActivityOnCreate(Insttrumentaion.java:1080)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     ... 11 more
    07-15 11:51:20.616: E/AndroidRuntime(2392): Caused by: java.lang.reflect.InvocationTargetException
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at java.lang.reflect.Constructor.constructNative(Native Method)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.view.LayoutInflater.createView(LayoutInflater.java:587)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     ... 23 more
    07-15 11:51:20.616: E/AndroidRuntime(2392): Caused by: java.lang.OutOfMemoryError
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.content.res.Resources.loadDrawable(Resources.java:1965)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.view.View.<init>(View.java:3330)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.view.View.<init>(View.java:3259)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.view.ViewGroup.<init>(ViewGroup.java:425)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.widget.RelativeLayout.<init>(RelativeLayout.java:210)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     ... 26 more
Run Code Online (Sandbox Code Playgroud)

编辑: master_background的图像大小是1080*1920,大小为1.01 MB,这是否会影响"内存不足"错误?因为这只出现在模拟器中,而不是在实际设备中出现.我只想解释这个问题.非常感谢你.

int*_*thi 5

查看此Android开发人员链接.并将图像放在适当大小的适当文件夹中.当您没有适当大小的图像时,操作系统将尝试调整图像大小本身,这会导致内存不足异常.

https://developer.android.com/guide/practices/screens_support.html