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):
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).
- Verify these files exist in your
- Note: Your Button uses
@drawable/loginimageas a background—if this is a large bitmap, it might cause memory issues. Try replacing it with a simple shape drawable temporarily to test.
- Your layout references several drawables:
Fix the ImageView's MinHeight
- The
img_login_logoImageView hasandroid:minHeight="500dp"but itslayout_heightis set to100dp. This mismatch can cause layout calculation errors. Remove theminHeightattribute entirely and test again.
- The
Verify Font Resources
- You're using
@font/robotofor some TextViews.- Make sure the
roboto.ttf(or corresponding font files) are placed inres/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-seriftemporarily to rule this out).
- Make sure the
- You're using
Get the Full Error Stack
- The error log you provided cuts off—look for the full
Caused byline in Logcat. It will tell you exactly what's failing (e.g.,ResourceNotFoundExceptionfor a missing drawable,OutOfMemoryErrorfor a large image). This is the most critical piece of info to pinpoint the issue.
- The error log you provided cuts off—look for the full
Clean & Rebuild Your Project
- Sometimes cached resources cause inflation issues. Go to
Build > Clean ProjectthenBuild > Rebuild Projectto clear old artifacts.
- Sometimes cached resources cause inflation issues. Go to
Double-Check Manifest Configuration
- Confirm your
LoginScreenActivity is declared correctly inAndroidManifest.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>
- Confirm your
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
相关产品推荐
相关产品推荐

