Android – 二进制 XML 文件第 10 行:无法膨胀类 <unknown>

huangapple go评论64阅读模式
英文:

Android - Binary XML file line #10: Error inflating class <unknown>

问题

我正在尝试转到另一个活动,但应用程序崩溃并显示以下错误信息。
我有两个活动:

  1. 聊天活动(ChatActivity)
  2. 好友个人资料查看活动(FriendProfileViewActivity)

以下是聊天活动的 XML 代码:

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appbarlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="-10dp"
    android:layout_marginTop="-10dp"
    android:layout_marginEnd="-10dp"
    android:layout_marginBottom="-10dp"
    android:padding="5dp">

    <!-- 这里是工具栏和其他视图的定义 -->
</com.google.android.material.appbar.AppBarLayout>

以下是聊天活动的 Java 代码:

RelativeLayout relativeLayout;
relativeLayout = findViewById(R.id.viewProfile);
relativeLayout.setOnClickListener(v -> {
    Intent intent1 = new Intent(ChatActivity.this, FriendProfileViewActivity.class);
    intent1.putExtra("friendId", friendId);
    startActivity(intent1);
});

以下是好友个人资料查看活动的 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:clipToPadding="false"
    android:padding="10dp"
    tools:context=".Activities.FriendProfileViewActivity">

    <!-- 这里是好友个人资料查看活动的布局定义 -->
</RelativeLayout>

以下是好友个人资料查看活动的 Java 代码:

public class FriendProfileViewActivity extends AppCompatActivity {

    TextView name, about, phoneNumber;
    Button blockUser;
    CircleImageView profilepic;
    DatabaseReference databaseReference;
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    String friendId, friendPhoneNumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friend_profile_view);

        // 这里是好友个人资料查看活动的 Java 代码
    }
}

请注意,由于您要求只返回翻译的部分,因此我只提供了与 XML 和 Java 代码相关的部分,不包括其他信息。如果您需要更多帮助或有其他问题,请随时提问。

英文:

I am trying to go to another activity but the app crashes with these errors.
I have 2 activity:

  1. Acivity Chat
  2. FriendProfileViewActivity

> java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mycompany.newchatapp/com.mycompany.newchatapp.Activities.FriendProfileViewActivity}: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class <unknown>
> Caused by: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class <unknown>
> Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class <unknown>
> Caused by: java.lang.reflect.InvocationTargetException

Here is the XML Code of ChatActivity.

 &lt;com.google.android.material.appbar.AppBarLayout
    android:id=&quot;@+id/appbarlayout&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_marginStart=&quot;-10dp&quot;
    android:layout_marginTop=&quot;-10dp&quot;
    android:layout_marginEnd=&quot;-10dp&quot;
    android:layout_marginBottom=&quot;-10dp&quot;
    android:padding=&quot;5dp&quot;&gt;


    &lt;androidx.appcompat.widget.Toolbar
        android:id=&quot;@+id/toolbar&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;&gt;

        &lt;ImageButton
            android:layout_width=&quot;40dp&quot;
            android:layout_height=&quot;50dp&quot;
            android:layout_marginEnd=&quot;10dp&quot;
            android:background=&quot;@android:color/transparent&quot;
            android:src=&quot;@drawable/ic_back&quot;
            android:scaleType=&quot;centerCrop&quot;/&gt;

        &lt;RelativeLayout
            android:id=&quot;@+id/viewProfile&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:gravity=&quot;center_vertical&quot;&gt;

            &lt;de.hdodenhof.circleimageview.CircleImageView
                android:id=&quot;@+id/friendpic&quot;
                android:layout_width=&quot;70dp&quot;
                android:layout_height=&quot;70dp&quot;
                android:layout_alignParentStart=&quot;true&quot;
                android:layout_marginStart=&quot;-5dp&quot;
                android:layout_marginEnd=&quot;20dp&quot;
                android:src=&quot;@drawable/ic_user&quot; /&gt;

            &lt;TextView
                android:id=&quot;@+id/friendname&quot;
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:layout_toEndOf=&quot;@+id/friendpic&quot;
                android:fontFamily=&quot;@font/alegreya_sc_italic&quot;
                android:text=&quot;Username&quot;
                android:textSize=&quot;24sp&quot; /&gt;

            &lt;TextView
                android:id=&quot;@+id/status&quot;
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:layout_below=&quot;@+id/friendname&quot;
                android:layout_toEndOf=&quot;@+id/friendpic&quot;
                android:text=&quot;offline&quot;
                android:textSize=&quot;20sp&quot; /&gt;
        &lt;/RelativeLayout&gt;
    &lt;/androidx.appcompat.widget.Toolbar&gt;
&lt;/com.google.android.material.appbar.AppBarLayout&gt;

JAVA CODE for Chat

RelativeLayout relativeLayout;
relativeLayout = findViewById(R.id.viewProfile);
relativeLayout.setOnClickListener(v -&gt; {
    Intent intent1 = new Intent(ChatActivity.this, FriendProfileViewActivity.class);
    intent1.putExtra(&quot;friendId&quot;, friendId);
    startActivity(intent1);
    });

XML code for FriendProfileViewActivity XML

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:clipToPadding=&quot;false&quot;
    android:padding=&quot;10dp&quot;
    tools:context=&quot;.Activities.FriendProfileViewActivity&quot;&gt;
&lt;de.hdodenhof.circleimageview.CircleImageView
    android:id=&quot;@+id/friendProfilePic&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;300dp&quot;
    android:layout_marginBottom=&quot;30dp&quot;
    android:scaleType=&quot;fitCenter&quot; /&gt;

&lt;TextView
    android:id=&quot;@+id/friendProfileName&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_below=&quot;@+id/friendProfilePic&quot;
    android:layout_marginBottom=&quot;10dp&quot;
    android:gravity=&quot;center&quot;
    android:text=&quot;Friend Name&quot;
    android:textSize=&quot;28sp&quot;
    android:textStyle=&quot;bold|italic&quot; /&gt;

&lt;View
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;1dp&quot;
    android:layout_below=&quot;@+id/friendProfileName&quot;
    android:layout_marginStart=&quot;-10dp&quot;
    android:layout_marginEnd=&quot;-10dp&quot;
    android:background=&quot;@android:color/black&quot; /&gt;

&lt;TextView
    android:id=&quot;@+id/about&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_below=&quot;@+id/friendProfileName&quot;
    android:layout_marginTop=&quot;10dp&quot;
    android:text=&quot;About Me: &quot;
    android:textSize=&quot;24sp&quot;
    android:textStyle=&quot;bold|italic&quot; /&gt;

&lt;TextView
    android:id=&quot;@+id/friendAboutMe&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_below=&quot;@+id/friendProfileName&quot;
    android:layout_marginTop=&quot;10dp&quot;
    android:layout_marginBottom=&quot;10dp&quot;
    android:layout_toEndOf=&quot;@+id/about&quot;
    android:text=&quot;@string/status&quot;
    android:textSize=&quot;26sp&quot;
    android:textStyle=&quot;italic&quot; /&gt;

&lt;View
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;1dp&quot;
    android:layout_below=&quot;@+id/friendAboutMe&quot;
    android:layout_marginStart=&quot;-10dp&quot;
    android:layout_marginEnd=&quot;-10dp&quot;
    android:background=&quot;@android:color/black&quot; /&gt;

&lt;TextView
    android:id=&quot;@+id/phoneNumber&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_below=&quot;@+id/friendAboutMe&quot;
    android:layout_marginTop=&quot;10dp&quot;
    android:text=&quot;Phone Number: &quot;
    android:textSize=&quot;24sp&quot;
    android:textStyle=&quot;bold|italic&quot; /&gt;

&lt;TextView
    android:id=&quot;@+id/friendPhoneNumber&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_below=&quot;@+id/friendAboutMe&quot;
    android:layout_marginTop=&quot;10dp&quot;
    android:layout_toEndOf=&quot;@+id/phoneNumber&quot;
    android:text=&quot;@string/fakeNumber&quot;
    android:textSize=&quot;26sp&quot;
    android:textStyle=&quot;italic&quot; /&gt;

&lt;View
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;1dp&quot;
    android:layout_below=&quot;@+id/friendPhoneNumber&quot;
    android:layout_marginStart=&quot;-10dp&quot;
    android:layout_marginTop=&quot;10dp&quot;
    android:layout_marginEnd=&quot;-10dp&quot;
    android:background=&quot;@android:color/black&quot; /&gt;

&lt;Button
    android:id=&quot;@+id/blockUser&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_alignParentBottom=&quot;true&quot;
    android:layout_centerHorizontal=&quot;true&quot;
    android:background=&quot;@drawable/button&quot;
    android:text=&quot;Block User&quot;
    android:textColor=&quot;@android:color/white&quot;
    android:textSize=&quot;24sp&quot;
    android:textStyle=&quot;italic&quot; /&gt;

</RelativeLayout>

JAVA CODE FriendProfileViewActivity

public class FriendProfileViewActivity extends AppCompatActivity {

    TextView name, about, phoneNumber;
    Button blockUser;
    CircleImageView profilepic;
    DatabaseReference databaseReference;
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    String friendId, friendPhoneNumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friend_profile_view);

        profilepic = findViewById(R.id.friendProfilePic);
        name = findViewById(R.id.friendProfileName);
        about = findViewById(R.id.friendAboutMe);
        phoneNumber = findViewById(R.id.friendPhoneNumber);
        blockUser = findViewById(R.id.blockUser);

        Intent intent = getIntent();
        friendId = intent.getStringExtra(&quot;friendId&quot;);

        databaseReference = FirebaseDatabase.getInstance().getReference(&quot;Users&quot;).child(friendId);
        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                if (snapshot.exists()) {
                    Users users = snapshot.getValue(Users.class);
                    name.setText(users.getUsername());
                    about.setText(users.getAboutMe());
                    phoneNumber.setText(users.getFullPhoneNumber());
                   // Glide.with(FriendProfileViewActivity.this).load(users.getProfilephotoURL())
                       //     .fitCenter().placeholder(R.drawable.ic_user).into(profilepic);
                    friendPhoneNumber = users.getFullPhoneNumber();
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
        blockUser.setOnClickListener(v -&gt; {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle(&quot;Block User&quot;);
            builder.setIcon(R.drawable.ic_block);
            builder.setCancelable(false);
            builder.setMessage(&quot;Do you wan to block User?&quot;);
            builder.setPositiveButton(&quot;Yes&quot;, (dialog, which) -&gt; {

                BlockList blockList = new BlockList(friendPhoneNumber);
                DatabaseReference blockUser = FirebaseDatabase.getInstance().
                        getReference(&quot;BlockList&quot;).child(user.getUid());
                blockUser.child(friendId).setValue(blockList);

                BlockList blockList1 = new BlockList(user.getPhoneNumber());
                blockUser = FirebaseDatabase.getInstance().
                        getReference(&quot;BlockList&quot;).child(friendId);
                blockUser.child(user.getUid()).setValue(blockList1);

                databaseReference = FirebaseDatabase.getInstance().getReference().child(&quot;ChatsList&quot;)
                        .child(user.getUid());
                databaseReference.child(friendId).removeValue();
                DatabaseReference friendChatList = FirebaseDatabase.getInstance().getReference().child(&quot;ChatsList&quot;)
                        .child(friendId);
                friendChatList.child(user.getUid()).removeValue();
                Intent intent1 = new Intent(FriendProfileViewActivity.this, MainScreen.class);
                startActivity(intent1);

            });
            builder.setNegativeButton(&quot;Cancel&quot;, (dialog, which) -&gt; dialog.dismiss());
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
        });
    }
}

答案1

得分: 1

错误堆栈如下:

由: android.view.InflateException引发: 二进制XML文件行#8: 二进制XML文件行#8: 膨胀de.hdodenhof.circleimageview.CircleImageView类时出错
引起: android.view.InflateException: 二进制XML文件行#8: 膨胀de.hdodenhof.circleimageview.CircleImageView类时出错
引起: java.lang.reflect.InvocationTargetException
在java.lang.reflect.Constructor.newInstance0(本机方法)处
在java.lang.reflect.Constructor.newInstance(Constructor.java:430)处
在android.view.LayoutInflater.createView(LayoutInflater.java:656)处
在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:798)处
在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:738)处
在android.view.LayoutInflater.rInflate(LayoutInflater.java:869)处
在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:832)处
在android.view.LayoutInflater.inflate(LayoutInflater.java:518)处
在android.view.LayoutInflater.inflate(LayoutInflater.java:426)处
在android.view.LayoutInflater.inflate(LayoutInflater.java:377)处
在androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)处
在androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)处
在za.co.Test.testit.MainActivity.onCreate(MainActivity.java:12)处
在android.app.Activity.performCreate(Activity.java:6910)处
在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)处
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2757)处
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2875)处
在android.app.ActivityThread.-wrap12(ActivityThread.java)处
在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1578)处
在android.os.Handler.dispatchMessage(Handler.java:105)处
在android.os.Looper.loop(Looper.java:156)处
在android.app.ActivityThread.main(ActivityThread.java:6623)处
在java.lang.reflect.Method.invoke(本机方法)处
在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)处
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)处
引起: java.lang.IllegalArgumentException: 不支持ScaleType FIT_CENTER。
在de.hdodenhof.circleimageview.CircleImageView.setScaleType(CircleImageView.java:134)处

从friendProfilePic中删除scale type修复了错误。

此外,请注意库的文档中提到:ScaleType始终为CENTER_CROP,如果尝试更改它,将会引发异常。这是(当前)设计如此,因为对于个人资料图像来说是完全合适的。

请确保仔细阅读您的堆栈跟踪(如果这是问题的原因),这将为您节省大量的烦恼。

英文:

Ok, I quickly ran your code and got THIS stack trace:

 Caused by: android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class de.hdodenhof.circleimageview.CircleImageView
     Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class de.hdodenhof.circleimageview.CircleImageView
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
        at android.view.LayoutInflater.createView(LayoutInflater.java:656)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:798)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:738)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:869)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:832)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
        at za.co.Test.testit.MainActivity.onCreate(MainActivity.java:12)
        at android.app.Activity.performCreate(Activity.java:6910)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2757)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2875)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1578)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:156)
        at android.app.ActivityThread.main(ActivityThread.java:6623)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
     Caused by: java.lang.IllegalArgumentException: ScaleType FIT_CENTER not supported.
        at de.hdodenhof.circleimageview.CircleImageView.setScaleType(CircleImageView.java:134)

Removing scale type from friendProfilePic fixed the error.

To add to this, they do make a note of this in the documentation of the library: The ScaleType is always CENTER_CROP and you'll get an exception if you try to change it. This is (currently) by design as it's perfectly fine for profile images.

Please make sure to read your stacktraces carefully (if this was the issue) it will save you tons of grief Android – 二进制 XML 文件第 10 行:无法膨胀类 <unknown>

huangapple
  • 本文由 发表于 2020年8月7日 16:58:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63298537.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定