在我移动布局中的一些元素之后,它才开始发生。如果您认为这有助于解决问题,我也可以共享XML代码。
这是引起异常的活动:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:p1="http://schemas.android.com/apk/res/android"
p1:layout_width="match_parent"
p1:layout_height="match_parent"
p1:id="@+id/relativeLayout1"
p1:background="@drawable/pickcitybackground">
<ImageView
p1:src="@drawable/pick"
p1:layout_width="60dp"
p1:layout_height="60dp"
p1:id="@+id/pick"
p1:layout_marginLeft="50dp"
p1:layout_marginTop="90dp" />
<ImageButton
p1:src="@drawable/austinbutton"
p1:layout_width="60dp"
p1:layout_height="60dp"
p1:id="@+id/austinButton"
p1:background="#02ffffff"
p1:scaleType="centerCrop"
p1:layout_marginTop="90dp"
p1:layout_marginLeft="210dp" />
<ImageButton
p1:src="@drawable/nashvillebutton"
p1:layout_width="60dp"
p1:layout_height="60dp"
p1:id="@+id/nashvilleButton"
p1:scaleType="centerCrop"
p1:background="#02ffffff"
p1:layout_marginLeft="50dp"
p1:layout_marginTop="265dp" />
<ImageButton
p1:src="@drawable/newyorkbutton"
p1:layout_width="60dp"
p1:layout_height="60dp"
p1:id="@+id/newyorkButton"
p1:scaleType="centerCrop"
p1:background="#02ffffff"
p1:layout_marginTop="265dp"
p1:layout_marginLeft="210dp" />
<ImageButton
p1:src="@drawable/sanfranbutton"
p1:layout_width="60dp"
p1:layout_height="60dp"
p1:id="@+id/sanfranButton"
p1:scaleType="centerCrop"
p1:background="#02ffffff"
p1:layout_marginTop="435dp"
p1:layout_marginLeft="50dp" />
<ImageButton
p1:src="@drawable/seattlebutton"
p1:layout_width="60dp"
p1:layout_height="60dp"
p1:id="@+id/seattleButton"
p1:layout_marginLeft="210dp"
p1:background="#02ffffff"
p1:scaleType="centerCrop"
p1:layout_marginTop="435dp" />
堆栈跟踪:
Android.Views.InflateException: Binary XML file line #1: Error inflating class android.widget.ImageButton ---> Java.Lang.Reflect.InvocationTargetException: ---> Java.Lang.OutOfMemoryError: Failed to allocate a 182250012 byte allocation with 4194304 free bytes and 74MB until OOM
at java.lang.OutOfMemoryError: Failed to allocate a 182250012 byte allocation with 4194304 free bytes and 74MB until OOM
at at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:988)
at at android.content.res.Resources.loadDrawableForCookie(Resources.java:2474)
at at android.content.res.Resources.loadDrawable(Resources.java:2381)
at at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
at at android.widget.ImageView.<init>(ImageView.java:146)
at at android.widget.ImageButton.<init>(ImageButton.java:86)
at at android.widget.ImageButton.<init>(ImageButton.java:82)
at at android.widget.ImageButton.<init>(ImageButton.java:78)
at at java.lang.reflect.Constructor.newInstance(Native Method)
at at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
at at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)
at at android.app.Activity.setContentView(Activity.java:2145)
at at md54421ba596fa8cc5324701d9bb904102d.pickCityPage.n_onCreate(Native Method)
at at md54421ba596fa8cc5324701d9bb904102d.pickCityPage.onCreate(pickCityPage.java:29)
at at android.app.Activity.performCreate(Activity.java:5990)
at at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2303)
at at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2415)
at at android.app.ActivityThread.access$900(ActivityThread.java:152)
at at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1328)
at at android.os.Handler.dispatchMessage(Handler.java:102)
at at android.os.Looper.loop(Looper.java:135)
at at android.app.ActivityThread.main(ActivityThread.java:5301)
at at java.lang.reflect.Method.invoke(Native Method)
at at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:723)
at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:613)
--- End of inner exception stack trace ---
at java.lang.reflect.InvocationTargetException
at at java.lang.reflect.Constructor.newInstance(Native Method)
at at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
at at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)
at at android.app.Activity.setContentView(Activity.java:2145)
at at md54421ba596fa8cc5324701d9bb904102d.pickCityPage.n_onCreate(Native Method)
at at md54421ba596fa8cc5324701d9bb904102d.pickCityPage.onCreate(pickCityPage.java:29)
at at android.app.Activity.performCreate(Activity.java:5990)
at at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2303)
at at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2415)
at at android.app.ActivityThread.access$900(ActivityThread.java:152)
at at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1328)
at at android.os.Handler.dispatchMessage(Handler.java:102)
at at android.os.Looper.loop(Looper.java:135)
at at android.app.ActivityThread.main(ActivityThread.java:5301)
at at java.lang.reflect.Method.invoke(Native Method)
at at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:723)
at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:613)
at Caused by: java.lang.OutOfMemoryError: Failed to allocate a 182250012 byte allocation with 4194304 free bytes and 74MB until OOM
at at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:988)
at at android.content.res.Resources.loadDrawableForCookie(Resources.java:2474)
at at android.content.res.Resources.loadDrawable(Resources.java:2381)
at at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
at at android.widget.ImageView.<init>(ImageView.java:146)
at at android.widget.ImageButton.<init>(ImageButton.java:86)
at at android.widget.ImageButton.<init>(ImageButton.java:82)
at at android.widget.ImageButton.<init>(ImageButton.java:78)
at ... 25 more
--- End of inner exception stack trace ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3819/96c7ba6c/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143
at Java.Interop.JniEnvironment+InstanceMethods.CallNonvirtualVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x000a7] in /Users/builder/data/lanes/3819/96c7ba6c/source/Java.Interop/src/Java.Interop/Java.Interop/JniEnvironment.g.cs:12083
at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00068] in /Users/builder/data/lanes/3819/96c7ba6c/source/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.JniInstanceMethods_Invoke.cs:31
at Android.App.Activity.SetContentView (System.Int32 layoutResID) [0x00017] in /Users/builder/data/lanes/3819/96c7ba6c/source/monodroid/src/Mono.Android/platforms/android-24/src/generated/Android.App.Activity.cs:5253
at Quickies.Android.pickCityPage.OnCreate (Android.OS.Bundle bundle) [0x0000e] in /Users/michaelsloan/Desktop/Quickies/Android/Quickies/pickCityPage.cs:28
at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x00011] in /Users/builder/data/lanes/3819/96c7ba6c/source/monodroid/src/Mono.Android/platforms/android-24/src/generated/Android.App.Activity.cs:2550
at at (wrapper dynamic-method) System.Object:e8fa207e-6c2f-4c69-899e-9bfde46df162 (intptr,intptr,intptr)
at android.view.InflateException: Binary XML file line #1: Error inflating class android.widget.ImageButton
at at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
at at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)
at at android.app.Activity.setContentView(Activity.java:2145)
at at md54421ba596fa8cc5324701d9bb904102d.pickCityPage.n_onCreate(Native Method)
at at md54421ba596fa8cc5324701d9bb904102d.pickCityPage.onCreate(pickCityPage.java:29)
at at android.app.Activity.performCreate(Activity.java:5990)
at at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2303)
at at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2415)
at at android.app.ActivityThread.access$900(ActivityThread.java:152)
at at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1328)
at at android.os.Handler.dispatchMessage(Handler.java:102)
at at android.os.Looper.loop(Looper.java:135)
at at android.app.ActivityThread.main(ActivityThread.java:5301)
at at java.lang.reflect.Method.invoke(Native Method)
at at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:723)
at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:613)
at Caused by: java.lang.reflect.InvocationTargetException
at at java.lang.reflect.Constructor.newInstance(Native Method)
at at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at ... 23 more
at Caused by: java.lang.OutOfMemoryError: Failed to allocate a 182250012 byte allocation with 4194304 free bytes and 74MB until OOM
at at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:988)
at at android.content.res.Resources.loadDrawableForCookie(Resources.java:2474)
at at android.content.res.Resources.loadDrawable(Resources.java:2381)
at at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
at at android.widget.ImageView.<init>(ImageView.java:146)
at at android.widget.ImageButton.<init>(ImageButton.java:86)
at at android.widget.ImageButton.<init>(ImageButton.java:82)
at at android.widget.ImageButton.<init>(ImageButton.java:78)
at ... 25 more
答案 0 :(得分:0)
您的布局中有一些需要大量内存的东西。可能是位图,我不知道没有XML布局。
OutOfMemoryError:无法分配带有4194304的182250012字节分配
答案 1 :(得分:0)
您的图片太大了。你应该考虑使用一个图像加载器库(我推荐Glide)加载图像,而不是让Android做它(因为Android非常糟糕。)
编辑:我的GlideImageView
:
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
/**
* Created by Zhuinden on 2016.05.07..
*/
public class GlideImageView
extends ImageView {
public GlideImageView(Context context) {
super(context);
init(context, null, 0);
}
public GlideImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0);
}
public GlideImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
@TargetApi(21)
public GlideImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs, defStyleAttr);
}
int drawableResource = 0;
private void init(Context context, AttributeSet attributeSet, int defStyle) {
TypedArray a = null;
if(defStyle != 0) {
a = getContext().obtainStyledAttributes(attributeSet, R.styleable.GlideImageView, defStyle, 0);
} else {
a = getContext().obtainStyledAttributes(attributeSet, R.styleable.GlideImageView);
}
drawableResource = a.getResourceId(0, 0);
a.recycle();
ViewUtils.waitForMeasure(this, new ViewUtils.OnMeasuredCallback() {
@Override
public void onMeasured(View view, int width, int height) {
if(!isInEditMode()) {
if(drawableResource != 0) {
Glide.with(getContext()).load(drawableResource).dontAnimate().into(GlideImageView.this);
}
} else {
setImageResource(drawableResource);
}
}
});
}
}
和
public class ViewUtils {
public interface OnMeasuredCallback {
void onMeasured(View view, int width, int height);
}
public static void waitForMeasure(final View view, final OnMeasuredCallback callback) {
int width = view.getWidth();
int height = view.getHeight();
if(width > 0 && height > 0) {
callback.onMeasured(view, width, height);
return;
}
view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
final ViewTreeObserver observer = view.getViewTreeObserver();
if(observer.isAlive()) {
observer.removeOnPreDrawListener(this);
}
callback.onMeasured(view, view.getWidth(), view.getHeight());
return true;
}
});
}
private ViewUtils() {
}
}
和
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="GlideImageView">
<attr name="image_resource" format="integer"/>
</declare-styleable>
</resources>