1

在 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);
    }
}

这是下一个活动:

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;
}}

错误的原因是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" >

这只是一个棕色的图像,我只需要使用这个图像来使用我的设计师提供的资源(图像)。我只是不明白当我删除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

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

4

2 回答 2

5

检查这个 Android 开发者链接。并将图像放在适当大小的适当文件夹中。当您没有适当大小的图像时,操作系统将尝试自行调整图像大小,这会导致内存不足异常。

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

于 2013-07-15T12:12:43.267 回答
3

仅仅因为图像文件在磁盘上的大小为 1 MB,并不意味着它将在内存中占用相同数量的内存。

当将该图像文件转换为可绘制对象以添加为背景时,它将占用更多的内存。你需要做的是——

制作调整大小的版本(320*480、480*800、720*1280)大小的图像,并将它们分别放在 drawable-mdpi、-hdpi 和 -xhdpi 中。您不应该需要更高分辨率的图像作为背景。

于 2013-07-15T12:27:43.023 回答