2016-10-28 200 views
1

它只是在我移動佈局中的一些元素後纔剛剛開始發生。如果您認爲這有助於解決問題,我也可以共享XML代碼。我的Activity中是什麼導致了這個Android.Views.InflateException?

下面是導致異常的活動:

<?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

它涉及到的ImageButton ID。 – AndroidHacker

+0

張貼布局xml –

+0

我發佈了XML佈局。有任何想法嗎?我會喜歡一些方向!我正在閱讀如何[有效地加載大型位圖,所以我沒有得到這個例外](https://developer.xamarin.com/recipes/android/resources/general/load_large_bitmaps_efficiently/)@MarcoPierucci –

回答

0

你在你的佈局的東西,需要大量的內存。可能的位圖,我不知道沒有XML佈局。

OutOfMemoryError: Failed to allocate a 182250012 byte allocation with 4194304 free

0

您的圖片太大。您應該考慮使用的圖像加載器庫(我建議滑翔)來加載圖像,而不是讓Android的做

EDIT(因爲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> 
+0

這是我第一次聽說圖像加載器庫,但他們似乎不言自明。快速搜索引導我[X]這個(https://components.xamarin.com/gettingstarted/universalimageloader)。 –

+0

就我個人而言,我在使用Glide完成'onMeasure'後加載圖像的那一天創建了自定義視圖。從來沒有使用過'ImageView',因爲它很愚蠢,並且可能會用盡內存。我會發布代碼,但我是從我的手機,並在我的家尋求修復我的互聯網:) – EpicPandaForce

+0

我正在使用C#Xamarin我的應用程序,所以希望我可以使用您的解決方案的一些指導。 –

相關問題