在logcat中,原因像这样提到
10-21 19:23:51.751 20211-20211/? E/Zygote: v2
10-2119:23:51.751 20211-20211/?E/受精卵: 访问信息 : 0 10-21 19:23:53.771 20211-20211/com.example.testapp E/Android运行时:致命 异常:主要 进程: com.example.testapp, PID: 20211 java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.example.testapp/com.example.testapp.MainActivity}: android.view.InflateException: 二进制 XML 文件行 #2: 二进制 XML 文件行 #2: 膨胀类时出错 android.widget.RelativeLayout at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) at android.app.ActivityThread.access$1100(ActivityThread.java:223) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:7223) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 由以下原因引起:android.view.InflateException:二进制 XML 文件行 #2:二进制 XML 文件行 #2:膨胀类 android.widget.RelativeLayout 时出错 at android.view.LayoutInflater.inflate(LayoutInflater.java:551) at android.view.LayoutInflater.inflate(LayoutInflater.java:429) at android.view.LayoutInflater.inflate(LayoutInflater.java:380) at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696) at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170) at com.example.testapp.MainActivity.onCreate(MainActivity.java:17) at android.app.Activity.performCreate(Activity.java:6877) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) at android.app.ActivityThread.access$1100(ActivityThread.java:223) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:7223) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 原因:android.view.InflateException:二进制 XML 文件行 #2:膨胀类 android.widget.RelativeLayout 时出错 at android.view.LayoutInflater.createView(LayoutInflater.java:657) at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) at android.view.LayoutInflater.onCreateView(LayoutInflater.java:706) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:774) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716) at android.view.LayoutInflater.inflate(LayoutInflater.java:498) at android.view.LayoutInflater.inflate(LayoutInflater.java:429) at android.view.LayoutInflater.inflate(LayoutInflater.java:380) at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696) at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170) at com.example.testapp.MainActivity.onCreate(MainActivity.java:17) at android.app.Activity.performCreate(Activity.java:6877) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) at android.app.ActivityThread.access$1100(ActivityThread.java:223) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:7223) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 由: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.newInstance(Native Method) at android.view.LayoutInflater.createView(LayoutInflater.java:631) at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) at android.view.LayoutInflater.onCreateView(LayoutInflater.java:706) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:774) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)at android.view.LayoutInflater.inflate(LayoutInflater.java:498) at android.view.LayoutInflater.inflate(LayoutInflater.java:429) at android.view.LayoutInflater.inflate(LayoutInflater.java:380) at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696) at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170) at com.example.testapp.MainActivity.onCreate(MainActivity.java:17) at android.app.Activity.performCreate(Activity.java:6877) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) at android.app.ActivityThread.access$1100(ActivityThread.java:223) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:7223) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 原因:java.lang.OutOfMemory错误:在 OOM 之前,无法分配具有 4182480 个可用字节和 126MB 的 386564108 字节分配 at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:856) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:675) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:2230) at android.content.res.Resources.loadDrawableForCookie(Resources.java:4211) at android.content.res.Resources.loadDrawable(Resources.java:4085) at android.content.res.Resources.loadDrawable(Resources.java:3935) at android.content.res.TypedArray.getDrawable(TypedArray.java:886) 在android.view.View。(查看.java:4226) 在android.view.ViewGroup。(观点组.java:591) at android.widget.RelativeLayout.(相对布局.java:249) at android.widget.RelativeLayout.(相对布局.java:245) at android.widget.RelativeLayout.(相对布局.java:241) at java.lang.reflect.Constructor.newInstance(Native Method) at android.view.LayoutInflater.createView(LayoutInflater.java:631) at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) at android.view.LayoutInflater.onCreateView(LayoutInflater.java:706) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:774) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716) at android.view.LayoutInflater.inflate(LayoutInflater.java:498) at android.view.LayoutInflater.inflate(LayoutInflater.java:429) at android.view.LayoutInflater.inflate(LayoutInflater.java:380) at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696) at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170) at com.example.testapp.MainActivity.onCreate(MainActivity.java:17) at android.app.Activity.performCreate(Activity.java:6877) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) at android.app.ActivityThread.access$1100(ActivityThread.java:223) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:7223) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
错误在这里
setContentView(R.layout.activity_main);
错误是什么?
这是主要的.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/bk99"
tools:context=".MainActivity"
tools:ignore="ExtraText">
<ImageView
android:id="@+id/imageView"
android:layout_width="253dp"
android:layout_height="263dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="77dp"
android:layout_marginRight="77dp"
android:layout_marginBottom="411dp"
android:contentDescription="@string/todo"
app:srcCompat="@drawable/log" />
<EditText
android:id="@+id/username"
android:layout_width="211dp"
android:layout_height="42dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="99dp"
android:layout_marginRight="99dp"
android:layout_marginBottom="333dp"
android:background="#1E5F9F"
android:drawableLeft="@drawable/ic_baseline_account_circle_24"
android:ems="10"
android:hint="@string/user_name"
android:inputType="text"
android:text="@string/enter_your_name"
android:textColor="#ffffff"
android:textSize="19sp" />
<EditText
android:id="@+id/password"
android:layout_width="217dp"
android:layout_height="42dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="97dp"
android:layout_marginRight="97dp"
android:layout_marginBottom="262dp"
android:background="#1E5F9F"
android:drawableLeft="@drawable/ic_baseline_lock_24"
android:ems="10"
android:hint="@string/password"
android:inputType="textPassword"
android:text="@string/password"
android:textColor="#ffffff"
android:textSize="19sp" />
<Button
android:layout_width="308dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="52dp"
android:layout_marginRight="52dp"
android:layout_marginBottom="141dp"
android:background="@drawable/rounded_corner"
android:text="@string/login"
android:textColor="#ffffff"
android:textSize="20sp" />
<CheckBox
android:id="@+id/reg"
android:layout_width="151dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="132dp"
android:layout_marginRight="132dp"
android:layout_marginBottom="82dp"
android:background="#1E5F9F"
android:onClick="onClick"
android:text="@string/remember_me"
android:textColor="#ffffff"
android:textSize="18sp"/>
</RelativeLayout>
如您的日志中所述Caused by: java.lang.OutOfMemoryError: Failed to allocate a 386564108 byte allocation with 4182480 free bytes and 126MB until OOM
您的应用程序可能会崩溃,因为您的图像尺寸太大。我建议尝试压缩您的图像文件或使用像 Glide 这样的图像住宿库。