问题
04-11 15:00:57.646 30603-30603/com.Blufish.blufish E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.Blufish.blufish, PID: 30603
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Blufish.blufish/com.Blufish.blufish.LoginActivity}: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class <unknown>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2487)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2547)
at android.app.ActivityThread.access$1100(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5603)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class <unknown>
at android.view.LayoutInflater.inflate(LayoutInflater.java:543)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.Blufish.blufish.LoginActivity.onCreate(LoginActivity.java:53)
at android.app.Activity.performCreate(Activity.java:6355)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2440)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2547)
at android.app.ActivityThread.access$1100(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5603)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:649)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:768)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:708)
at android.view.LayoutInflater.inflate(LayoutInflater.java:496)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.Blufish.blufish.LoginActivity.onCreate(LoginActivity.java:53)
at android.app.Activity.performCreate(Activity.java:6355)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2440)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2547)
at android.app.ActivityThread.access$1100(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5603)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at android.view.LayoutInflater.createView(LayoutInflater.java:623)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:768)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:708)
at android.view.LayoutInflater.inflate(LayoutInflater.java:496)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.Blufish.blufish.LoginActivity.onCreate(LoginActivity.java:53)
at android.app.Activity.performCreate(Activity.java:6355)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2440)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2547)
at android.app.ActivityThread.access$1100(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5603)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 864216012 byte allocation with 16777216 free bytes and 162MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:613)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:446)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
at android.content.res.Resources.createFromResourceStream(Resources.java:2952)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2684)
at android.content.res.Resources.loadDrawable(Resources.java:2580)
at android.content.res.MiuiResources.loadDrawable(MiuiResources.java:398)
at android.content.res.TypedArray.getDrawable(TypedArray.java:872)
at android.view.View.<init>(View.java:3963)
at android.view.ViewGroup.<init>(ViewGroup.java:574)
at android.view.ViewGroup.<init>(ViewGroup.java:570)
at android.view.ViewGroup.<init>(ViewGroup.java:566)
at androidx.constraintlayout.widget.ConstraintLayout.<init>(ConstraintLayout.java:575)
at java.lang.reflect.Constructor.newInstance(Native Method)
at android.view.LayoutInflater.createView(LayoutInflater.java:623)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:768)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:708)
at android.view.LayoutInflater.inflate(LayoutInflater.java:496)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.Blufish.blufish.LoginActivity.onCreate(LoginActivity.java:53)
at android.app.Activity.performCreate(Activity.java:6355)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2440)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2547)
at android.app.ActivityThread.access$1100(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5603)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
LoginActivity XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg1"
tools:context=".LoginActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:contentDescription="@string/desc_img"
android:visibility="visible"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/user" />
<EditText
android:id="@+id/username"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
android:layout_marginRight="32dp"
android:background="@drawable/text"
android:drawableLeft="@drawable/ic_username"
android:contentDescription="@string/desc_name"
android:ems="10"
android:hint="@string/login_username"
android:inputType="textEmailAddress"
android:textColor="#000000"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline" />
<EditText
android:id="@+id/password"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="32dp"
android:layout_marginRight="32dp"
android:background="@drawable/text"
android:drawableLeft="@drawable/ic_lpassword"
android:contentDescription="@string/desc_pass"
android:ems="10"
android:hint="@string/login_pass"
android:inputType="textPassword"
android:textColor="#000000"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/username" />
<Button
android:id="@+id/loginbutton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="64dp"
android:layout_marginLeft="64dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="64dp"
android:layout_marginRight="64dp"
android:background="@drawable/button"
android:contentDescription="@string/login"
android:text="@string/login"
android:textColor="#FFFFFF"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/password" />
<Button
android:id="@+id/signbutton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="64dp"
android:layout_marginLeft="64dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="64dp"
android:layout_marginRight="64dp"
android:background="@drawable/button"
android:contentDescription="@string/sign_up"
android:text="@string/sign_up"
android:textColor="#FFFFFF"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/loginbutton" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.40218878" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/loading"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/progressBar" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/signbutton"
app:srcCompat="@drawable/blufish" />
</androidx.constraintlayout.widget.ConstraintLayout>
This Error does not comes when i use phones with Android Oreo or higher Version. But, when I am trying this app in Android Marshmallow with 3GB RAM this error pops up.
Things I have tried while looking up in google are:
1) Added com.android.support:design:28.0.0
2) Added android:largeHeap="true"
3) Copied all of my drawable items in drawable-v24 folder
Above methods did not give any positive results.
回答1:
It seems that your drawable resource is such big that there is not enough memory for it to be loaded.
First, if it is not meant to be that high-resolution, I would recommend you to provide different images for each screen pixel densities. Check out this(Support different pixel densities) for more information.
Second, if you want to load that high-resolution image anyway, you should consider not loading it completely to the memory. Check out this(Loading Large Bitmaps Efficiently) for more information about loading a large image efficiently.
来源:https://stackoverflow.com/questions/61154757/android-view-inflateexception-binary-xml-file-line-2-binary-xml-file-line-2