Android 应用程序在 setContentView(R.layout.XXXXXX) 处崩溃;

Posted

技术标签:

【中文标题】Android 应用程序在 setContentView(R.layout.XXXXXX) 处崩溃;【英文标题】:Android app crashes at setContentView(R.layout.XXXXXX); 【发布时间】:2020-08-24 21:59:59 【问题描述】:

在执行 setContentView 行后,我的应用程序崩溃了。这是我的日志猫

 --------- beginning of crash
05-09 14:46:29.664 12307-12307/com.bitproject.driverapplication E/androidRuntime: FATAL EXCEPTION: main
    Process: com.bitproject.driverapplication, PID: 12307
    java.lang.RuntimeException: Unable to start activity ComponentInfocom.bitproject.driverapplication/com.bitproject.driverapplication.SettingsActivity: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class de.hdodenhof.circleimageview.CircleImageView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class de.hdodenhof.circleimageview.CircleImageView
        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 androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
        at com.bitproject.driverapplication.SettingsActivity.onCreate(SettingsActivity.java:37)
        at android.app.Activity.performCreate(Activity.java:6237)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
     Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class de.hdodenhof.circleimageview.CircleImageView
        at android.view.LayoutInflater.createView(LayoutInflater.java:645)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
        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.bitproject.driverapplication.SettingsActivity.onCreate(SettingsActivity.java:37) 
        at android.app.Activity.performCreate(Activity.java:6237) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at android.view.LayoutInflater.createView(LayoutInflater.java:619)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
        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.bitproject.driverapplication.SettingsActivity.onCreate(SettingsActivity.java:37) 
        at android.app.Activity.performCreate(Activity.java:6237) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
     Caused by: android.content.res.Resources$NotFoundException: Resource "com.bitproject.driverapplication:drawable/profile_image" (7f0700a1) is not a Drawable (color or path): TypedValuet=0x1/d=0x7f0700a1 a=-1 r=0x7f0700a1
        at android.content.res.Resources.loadDrawableForCookie(Resources.java:2602)
        at android.content.res.Resources.loadDrawable(Resources.java:2540)
        at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
        at android.widget.ImageView.<init>(ImageView.java:152)
        at android.widget.ImageView.<init>(ImageView.java:140)
        at de.hdodenhof.circleimageview.CircleImageView.<init>(CircleImageView.java:98)
        at de.hdodenhof.circleimageview.CircleImageView.<init>(CircleImageView.java:94)
        at java.lang.reflect.Constructor.newInstance(Native Method) 
        at android.view.LayoutInflater.createView(LayoutInflater.java:619) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
        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.bitproject.driverapplication.SettingsActivity.onCreate(SettingsActivity.java:37) 
        at android.app.Activity.performCreate(Activity.java:6237) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
05-09 14:46:29.892 12307-12375/com.bitproject.driverapplication V/FA: Recording user engagement, ms: 3084
05-09 14:46:29.897 12307-12375/com.bitproject.driverapplication V/FA: Activity paused, time: 64601668

我对android很陌生,我试图阅读日志并找到问题。但我失败了。有几个相关的问题,但不明白如何将它们与我的联系起来。

这是 Java 文件。

public class SettingsActivity extends AppCompatActivity 

    private Button updateAccountSettings;
    private EditText username;
    private CircleImageView userProfileImage;
    private String currentUserID;

    private FirebaseAuth mAuth;
    private DatabaseReference rootRef;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);               <----------------- App crashes at here

        mAuth = FirebaseAuth.getInstance();
        currentUserID = mAuth.getCurrentUser().getUid();

        rootRef = FirebaseDatabase.getInstance().getReference().child("Users").child("Driver");

        updateAccountSettings =  (Button) findViewById(R.id.update_settings_button);
        username = (EditText) findViewById(R.id.set_user_name);
        userProfileImage = (CircleImageView) findViewById(R.id.set_profile_image);

        updateAccountSettings.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                updateSettings();
            
        );
    

    private void updateSettings() 
        String setUsername = username.getText().toString();

        if (TextUtils.isEmpty(setUsername)) 
            Toast.makeText(this, "Please enter your user name...",Toast.LENGTH_SHORT).show();
        
        else 
            HashMap<String, String> profileMap = new HashMap<>();
            profileMap.put("UserID", currentUserID);
            profileMap.put("Name", setUsername);

            rootRef.child(currentUserID).setValue(profileMap)
                    .addOnCompleteListener(new OnCompleteListener<Void>() 
                        @Override
                        public void onComplete(@NonNull Task<Void> task) 
                            if (task.isSuccessful()) 
                                Intent intent = new Intent(SettingsActivity.this, MessageActivity.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                startActivity(intent);
                                finish();

                                Toast.makeText(SettingsActivity.this, "Profile Updates Successfully!", Toast.LENGTH_SHORT).show();
                            
                            else 
                                String message = task.getException().toString();
                                Toast.makeText(SettingsActivity.this, "Error! " + message, Toast.LENGTH_SHORT).show();
                            
                        
                    );
        
    

我有另一个名为 mainActivity 的活动。如果在 firebase 的 currentUserId 下没有用户名,则 mainActivity 将具有 settingsActivity 的意图(上面提供了 java 编码)。当它得到意图时,就会发生错误。

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_
    android:layout_
    android:background="@color/colorPrimaryLight"
    tools:context=".SettingsActivity">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/set_profile_image"
        android:layout_
        android:layout_
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="45dp"
        android:src="@drawable/profile_image"
        app:civ_border_
        app:civ_border_color="@color/colorPrimary"/>

    <EditText
        android:id="@+id/set_user_name"
        android:layout_
        android:layout_
        android:layout_below="@+id/set_profile_image"
        android:inputType="textMultiLine"
        android:hint="Username"
        android:textSize="20sp"
        android:textAlignment="viewStart"
        android:drawablePadding="20dp"
        android:drawableStart="@drawable/edit_profile"
        android:textColor="@color/colorPrimaryDark"
        android:textStyle="bold"
        android:background="@drawable/edit_text_background"
        android:layout_marginStart="15dp"
        android:layout_marginEnd="15dp"
        android:padding="6dp"
        android:textColorHint="@color/colorSecondaryText"
        android:layout_marginTop="20dp"/>

    <Button
        android:id="@+id/update_settings_button"
        android:layout_
        android:layout_
        android:layout_below="@+id/set_user_name"
        android:inputType="textMultiLine"
        android:text="Update"
        android:textAlignment="center"
        android:textColor="@color/colorPrimaryText"
        android:textStyle="bold"
        android:background="@drawable/button_corner"
        android:layout_marginStart="50dp"
        android:layout_marginEnd="50dp"
        android:padding="6dp"
        android:layout_marginTop="10dp"
        android:textAllCaps="false"
        android:textSize="20sp"/>

</RelativeLayout>

【问题讨论】:

您的 XML 有问题 从您的 XML 中删除 CircleImageView。 引起:android.content.res.Resources$NotFoundException:资源“com.bitproject.driverapplication:drawable/profile_image”(7f0700a1)不是可绘制的(颜色或路径)。您确定 profile_image 是有效图像吗?您可以发布您的 activity_settings.xml 文件吗? 我已经更新了 XML 文件。 circleImageView 添加了implementation 'de.hdodenhof:circleimageview:3.1.0'的依赖 确保您的 profile_image drawable 在您的主 drawable 文件夹中,而不是在 drawable-v21 或任何其他文件夹中 【参考方案1】:

原因:android.content.res.Resources$NotFoundException: Resource “com.bitproject.driverapplication:drawable/profile_image”(7f0700a1) 不是可绘制对象(颜色或路径):TypedValuet=0x1/d=0x7f0700a1 a=-1 r=0x7f0700a1

仔细检查您的 logcat 中的这些行。我在我的环境中运行了您的代码。它工作得很好。膨胀CircleImageView没有问题。很明显,您的 @drawable/profile_image 不是有效的可绘制对象,可能不是受支持的图像文件或已损坏。使用有效的 drawable 将解决这个问题。

【讨论】:

【参考方案2】:

CircleImageView 中存在问题。删除它,您的问题将得到解决。检查其库是否正确导入。

【讨论】:

我不能在不删除的情况下解决问题吗?依赖行已在上面作为注释提到。你能帮我解决这个问题吗?

以上是关于Android 应用程序在 setContentView(R.layout.XXXXXX) 处崩溃;的主要内容,如果未能解决你的问题,请参考以下文章

Android培训Android课堂重点内容汇总

如何将 Google 标记设置为按钮?

解析GSON - 怎么样?

AsyncTask中的IOException

从“最近图像”文件夹中选择图像时,图片路径为空

如何在Android中启动JAVA程序