You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android布局膨胀异常:登录界面无法显示应用崩溃求助

Android Login Screen Crash: InflateException When Launching in Emulator

我开发的Android应用登录界面在模拟器里完全显示不出来,一启动就提示“Your application has been Stopped”。奇怪的是login_screen.xml在设计器里布局显示正常,而且我已经在Manifest文件里配置了LoginScreen,但错误还是反复出现。

Layout Code (login_screen.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/black" >

    <RelativeLayout
        android:id="@+id/relMyHead"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="@drawable/loginimage">

        <EditText
            android:id="@+id/edt_userName"
            android:layout_width="260dp"
            android:layout_height="48dp"
            android:layout_below="@+id/img_login_logo"
            android:layout_centerInParent="true"
            android:layout_marginLeft="6dp"
            android:layout_marginTop="150dp"
            android:drawableLeft="@drawable/envelope"
            android:drawablePadding="14dp"
            android:drawableStart="@drawable/envelope"
            android:hint="@string/Email"
            android:inputType="textEmailAddress"
            android:padding="4dp"
            android:singleLine="true"
            android:textColor="@color/blue_500"
            android:textColorHint="#BBBBBB">
            <requestFocus />
        </EditText>

        <EditText
            android:id="@+id/edt_password"
            android:layout_width="260dp"
            android:layout_height="48dp"
            android:layout_alignLeft="@+id/edt_userName"
            android:layout_below="@+id/edt_userName"
            android:layout_marginRight="6dp"
            android:layout_marginTop="10dp"
            android:drawableLeft="@drawable/key"
            android:drawablePadding="14dp"
            android:hint="@string/password"
            android:inputType="textPassword"
            android:padding="4dp"
            android:password="true"
            android:singleLine="true"
            android:textColor="@color/black"
            android:textColorHint="#BBBBBB" />

        <Button
            android:id="@+id/btn_Login"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_alignLeft="@+id/edt_password"
            android:layout_alignRight="@+id/edt_password"
            android:layout_below="@+id/edt_password"
            android:layout_marginTop="20dp"
            android:background="@drawable/loginimage"
            android:gravity="center"
            android:letterSpacing="0.1"
            android:text="@string/btn_login"
            android:textColor="@color/white"
            android:textStyle="bold"
            tools:foregroundTintMode="screen" />

        <ImageView
            android:id="@+id/img_login_logo"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="100dp"
            android:adjustViewBounds="true"
            android:contentDescription="@string/app_name"
            android:minHeight="500dp"
            android:scaleType="fitCenter"
            android:src="@drawable/csrlogo_2x" />

        <LinearLayout
            android:id="@+id/new_user_reg_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/btn_Login"
            android:layout_marginTop="25dp"
            android:gravity="center_horizontal"
            android:longClickable="false">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fontFamily="@font/roboto"
                android:letterSpacing="0.1"
                android:text="@string/not_member"
                android:textColor="@color/barLightColor"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/register"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:clickable="true"
                android:letterSpacing="0.1"
                android:textColor="@color/popup_text_color"
                android:textSize="20sp" />
        </LinearLayout>

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/img_login_logo"
            android:layout_centerHorizontal="true"
            android:fontFamily="@font/roboto"
            android:letterSpacing="0.2"
            android:text="TRAVEL ASSIST"
            android:textColor="@color/popup_text_color"
            android:textSize="25sp"
            android:textStyle="bold" />
    </RelativeLayout>
</RelativeLayout>

Error Log

Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class <unknown>
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393)
at android.app.Activity.setContentView(Activity.java:2166)
at com.example.developer.travelassistmobileapp.LoginScreen.onCreate(LoginScreen.java:51)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(A

Troubleshooting Steps & Fixes

Let's break down the most likely causes for this InflateException (since the error log is truncated, these are the top suspects based on your layout):

  1. Check Drawable Resources

    • Your layout references several drawables: @drawable/loginimage, @drawable/envelope, @drawable/key, @drawable/csrlogo_2x.
      • Verify these files exist in your res/drawable (or density-specific) folders.
      • Ensure filenames are all lowercase (Android is case-sensitive for resources) and have no spaces/special characters.
      • Check if the images are corrupted or in an unsupported format (stick to PNG/JPG for bitmaps).
    • Note: Your Button uses @drawable/loginimage as a background—if this is a large bitmap, it might cause memory issues. Try replacing it with a simple shape drawable temporarily to test.
  2. Fix the ImageView's MinHeight

    • The img_login_logo ImageView has android:minHeight="500dp" but its layout_height is set to 100dp. This mismatch can cause layout calculation errors. Remove the minHeight attribute entirely and test again.
  3. Verify Font Resources

    • You're using @font/roboto for some TextViews.
      • Make sure the roboto.ttf (or corresponding font files) are placed in res/font/.
      • If targeting API levels below 26, ensure you're using the Android Support Library's font compatibility features (or switch to a system font like sans-serif temporarily to rule this out).
  4. Get the Full Error Stack

    • The error log you provided cuts off—look for the full Caused by line in Logcat. It will tell you exactly what's failing (e.g., ResourceNotFoundException for a missing drawable, OutOfMemoryError for a large image). This is the most critical piece of info to pinpoint the issue.
  5. Clean & Rebuild Your Project

    • Sometimes cached resources cause inflation issues. Go to Build > Clean Project then Build > Rebuild Project to clear old artifacts.
  6. Double-Check Manifest Configuration

    • Confirm your LoginScreen Activity is declared correctly in AndroidManifest.xml:
      <activity android:name=".LoginScreen">
          <!-- If this is your launch activity, add the intent-filter -->
          <intent-filter>
              <action android:name="android.intent.action.MAIN" />
              <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
      </activity>
      

Start with these steps—chances are one of them will resolve the crash. If you can get the full error log, that will narrow it down even faster!

内容的提问来源于stack exchange,提问作者giri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:22:10