关于启动新活动的OutOfMemoryError

时间:2016-03-21 17:02:09

标签: android out-of-memory

我正尝试通过以下方式从其他活动启动活动:

Intent loginActivity = new Intent(mainAppContext,LoginActivity.class);
loginActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
loginActivity.putExtra("Message", mainAppContext.getResources().getString(R.string.RegisteringUserAPIFailure));
mainAppContext.startActivity(loginActivity);

新活动布局只有两张图片,其中一张ImageView为7kb,另一张ImageButton为11kb。哪个不应该生成OutOfMemoryError?

但这会产生以下错误:

03-21 21:20:14.318 20567-20567/com.administrator.myproject E/art: Throwing OutOfMemoryError "Failed to allocate a 108000012 byte allocation with 9062644 free bytes and 8MB until OOM"
03-21 21:20:14.383 20567-20567/com.administrator.myproject E/art: Throwing OutOfMemoryError "Failed to allocate a 108000012 byte allocation with 9062596 free bytes and 8MB until OOM"
03-21 21:20:14.423 20567-20567/com.administrator.myproject E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.administrator.myproject, PID: 20567
                                                                            java.lang.OutOfMemoryError: Failed to allocate a 108000012 byte allocation with 9062596 free bytes and 8MB until OOM
   at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
   at android.graphics.Bitmap.nativeCreate(Native Method)
   at android.graphics.Bitmap.createBitmap(Bitmap.java:942)
   at android.graphics.Bitmap.createBitmap(Bitmap.java:913)
   at android.graphics.Bitmap.createBitmap(Bitmap.java:880)
   at android.graphics.drawable.VectorDrawable$VectorDrawableState.createCachedBitmapIfNeeded(VectorDrawable.java:713)
   at android.graphics.drawable.VectorDrawable.draw(VectorDrawable.java:280)
   at android.widget.ImageView.onDraw(ImageView.java:1197)
   at android.view.View.draw(View.java:16536)
   at android.view.View.updateDisplayListIfDirty(View.java:15466)
   at android.view.View.getDisplayList(View.java:15488)
   at android.view.View.draw(View.java:16258)
   at android.view.ViewGroup.drawChild(ViewGroup.java:3713)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3506)
   at android.view.View.draw(View.java:16539)
   at android.view.View.updateDisplayListIfDirty(View.java:15466)
   at android.view.View.getDisplayList(View.java:15488)
   at android.view.View.draw(View.java:16258)
   at android.view.ViewGroup.drawChild(ViewGroup.java:3713)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3506)
   at android.view.View.draw(View.java:16539)
   at android.view.View.updateDisplayListIfDirty(View.java:15466)
   at android.view.View.getDisplayList(View.java:15488)
   at android.view.View.draw(View.java:16258)
   at android.view.ViewGroup.drawChild(ViewGroup.java:3713)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3506)
   at android.view.View.updateDisplayListIfDirty(View.java:15461)
   at android.view.View.getDisplayList(View.java:15488)
   at android.view.View.draw(View.java:16258)
   at android.view.ViewGroup.drawChild(ViewGroup.java:3713)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3506)
   at android.view.View.updateDisplayListIfDirty(View.java:15461)
   at android.view.View.getDisplayList(View.java:15488)
   at android.view.View.draw(View.java:16258)
   at android.view.ViewGroup.drawChild(ViewGroup.java:3713)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3506)
   at android.view.View.updateDisplayListIfDirty(View.java:15461)
   at android.view.View.getDisplayList(View.java:15488)
   at android.view.View.draw(View.java:16258)
   at android.view.ViewGroup.drawChild(ViewGroup.java:3713)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3506)
   at android.view.View.updateDisplayListIfDirty(View.java:15461)
   at android.view.View.getDisplayList(View.java:15488)
   at android.view.View.draw(View.java:16258)
   at android.view.ViewGroup.drawChild(ViewGroup.java:3713)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3506)
   at android.view.View.draw(View.java:16539)
   at android.widget.FrameLayout.draw(FrameLayout.java:598)
   at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:3111)
   at android.view.View.updateDisplayListIfDirty(View.java:15466)
   at android.view.View.getDisplayList(View.java:15488)
   at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:309)
   at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:315)
   at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:354)
   at android.view.ViewRootImpl.draw(ViewRootImpl.java:2942)
   at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2739)
   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2325)
   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1301)
   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7021)
   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
   at android.view.Choreographer.doCallbacks(Choreographer.java:590)
   at android.view.Choreographer.doFrame(Choreographer.java:560)
   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
   at android.os.Handler.handleCallback(Handler.java:739)
                                                                                at a

-

<ImageView
            android:id="@+id/imageView1"
            android:layout_width="@dimen/dimen_8"
            android:layout_height="@dimen/dimen_8"
            android:layout_below="@+id/login_progress"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/application_logo_color" />

        <ImageButton
            android:id="@+id/gplus_sign_in_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="@dimen/margin_1"
            android:background="@color/transparent"
            android:elevation="8dp"
            android:src="@drawable/google_plus_sign_in_button" />


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

    ImageButton gPlusSignInButton = (ImageButton) findViewById(R.id.gplus_sign_in_button);
    SnackBarView = (View) gPlusSignInButton;
    gPlusSignInButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(SnackBarView, getResources().getString(R.string.SendingGoogleSignIn), Snackbar.LENGTH_INDEFINITE).setAction("Action", null).show();
            googlePlusSignIn();
        }
    });

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        String message = extras.getString("Message");
        Snackbar.make(SnackBarView, message, Snackbar.LENGTH_INDEFINITE).setAction("Action", null).show();
    }
}

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="2500dp"
        android:height="1200dp"
        android:viewportWidth="2500.0"
        android:viewportHeight="1200.0">

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="@dimen/dimen_8"
        android:height="51dp"
        android:viewportWidth="346.0"
        android:viewportHeight="72.0">

3 个答案:

答案 0 :(得分:5)

看起来你正在使用矢量绘图。

确保它们具有合理的固有尺寸(宽度和高度)

e.g:

<vector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:viewportWidth="512" android:viewportHeight="512"
    android:width="64dp" android:height="64dp">

如果您将尺寸设置得太大,当系统尝试显示它们时,它们将抛出内存不足错误,因为它将首先尝试以您设置的尺寸创建它们。

答案 1 :(得分:0)

可能图像非常大,试着让它们变小。或者阅读this article http://developer.android.com/intl/es/training/displaying-bitmaps/load-bitmap.html

答案 2 :(得分:-4)

只需添加更高的HeapSize,大多数应用都无法使用默认堆,在app / buid.gradle中添加:

android { 
    ...
    dexOptions {
        javaMaxHeapSize "8g"
        incremental true
    }
    ...
}