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

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

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

问题

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

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

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

  1. <com.google.android.material.appbar.AppBarLayout
  2. android:id="@+id/appbarlayout"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:layout_marginStart="-10dp"
  6. android:layout_marginTop="-10dp"
  7. android:layout_marginEnd="-10dp"
  8. android:layout_marginBottom="-10dp"
  9. android:padding="5dp">
  10. <!-- 这里是工具栏和其他视图的定义 -->
  11. </com.google.android.material.appbar.AppBarLayout>

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

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

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:clipToPadding="false"
  7. android:padding="10dp"
  8. tools:context=".Activities.FriendProfileViewActivity">
  9. <!-- 这里是好友个人资料查看活动的布局定义 -->
  10. </RelativeLayout>

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

  1. public class FriendProfileViewActivity extends AppCompatActivity {
  2. TextView name, about, phoneNumber;
  3. Button blockUser;
  4. CircleImageView profilepic;
  5. DatabaseReference databaseReference;
  6. FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
  7. String friendId, friendPhoneNumber;
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_friend_profile_view);
  12. // 这里是好友个人资料查看活动的 Java 代码
  13. }
  14. }

请注意,由于您要求只返回翻译的部分,因此我只提供了与 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.

  1. &lt;com.google.android.material.appbar.AppBarLayout
  2. android:id=&quot;@+id/appbarlayout&quot;
  3. android:layout_width=&quot;match_parent&quot;
  4. android:layout_height=&quot;wrap_content&quot;
  5. android:layout_marginStart=&quot;-10dp&quot;
  6. android:layout_marginTop=&quot;-10dp&quot;
  7. android:layout_marginEnd=&quot;-10dp&quot;
  8. android:layout_marginBottom=&quot;-10dp&quot;
  9. android:padding=&quot;5dp&quot;&gt;
  10. &lt;androidx.appcompat.widget.Toolbar
  11. android:id=&quot;@+id/toolbar&quot;
  12. android:layout_width=&quot;match_parent&quot;
  13. android:layout_height=&quot;wrap_content&quot;&gt;
  14. &lt;ImageButton
  15. android:layout_width=&quot;40dp&quot;
  16. android:layout_height=&quot;50dp&quot;
  17. android:layout_marginEnd=&quot;10dp&quot;
  18. android:background=&quot;@android:color/transparent&quot;
  19. android:src=&quot;@drawable/ic_back&quot;
  20. android:scaleType=&quot;centerCrop&quot;/&gt;
  21. &lt;RelativeLayout
  22. android:id=&quot;@+id/viewProfile&quot;
  23. android:layout_width=&quot;wrap_content&quot;
  24. android:layout_height=&quot;wrap_content&quot;
  25. android:gravity=&quot;center_vertical&quot;&gt;
  26. &lt;de.hdodenhof.circleimageview.CircleImageView
  27. android:id=&quot;@+id/friendpic&quot;
  28. android:layout_width=&quot;70dp&quot;
  29. android:layout_height=&quot;70dp&quot;
  30. android:layout_alignParentStart=&quot;true&quot;
  31. android:layout_marginStart=&quot;-5dp&quot;
  32. android:layout_marginEnd=&quot;20dp&quot;
  33. android:src=&quot;@drawable/ic_user&quot; /&gt;
  34. &lt;TextView
  35. android:id=&quot;@+id/friendname&quot;
  36. android:layout_width=&quot;wrap_content&quot;
  37. android:layout_height=&quot;wrap_content&quot;
  38. android:layout_toEndOf=&quot;@+id/friendpic&quot;
  39. android:fontFamily=&quot;@font/alegreya_sc_italic&quot;
  40. android:text=&quot;Username&quot;
  41. android:textSize=&quot;24sp&quot; /&gt;
  42. &lt;TextView
  43. android:id=&quot;@+id/status&quot;
  44. android:layout_width=&quot;wrap_content&quot;
  45. android:layout_height=&quot;wrap_content&quot;
  46. android:layout_below=&quot;@+id/friendname&quot;
  47. android:layout_toEndOf=&quot;@+id/friendpic&quot;
  48. android:text=&quot;offline&quot;
  49. android:textSize=&quot;20sp&quot; /&gt;
  50. &lt;/RelativeLayout&gt;
  51. &lt;/androidx.appcompat.widget.Toolbar&gt;
  52. &lt;/com.google.android.material.appbar.AppBarLayout&gt;

JAVA CODE for Chat

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

XML code for FriendProfileViewActivity XML

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  4. android:layout_width=&quot;match_parent&quot;
  5. android:layout_height=&quot;match_parent&quot;
  6. android:clipToPadding=&quot;false&quot;
  7. android:padding=&quot;10dp&quot;
  8. tools:context=&quot;.Activities.FriendProfileViewActivity&quot;&gt;
  9. &lt;de.hdodenhof.circleimageview.CircleImageView
  10. android:id=&quot;@+id/friendProfilePic&quot;
  11. android:layout_width=&quot;match_parent&quot;
  12. android:layout_height=&quot;300dp&quot;
  13. android:layout_marginBottom=&quot;30dp&quot;
  14. android:scaleType=&quot;fitCenter&quot; /&gt;
  15. &lt;TextView
  16. android:id=&quot;@+id/friendProfileName&quot;
  17. android:layout_width=&quot;match_parent&quot;
  18. android:layout_height=&quot;wrap_content&quot;
  19. android:layout_below=&quot;@+id/friendProfilePic&quot;
  20. android:layout_marginBottom=&quot;10dp&quot;
  21. android:gravity=&quot;center&quot;
  22. android:text=&quot;Friend Name&quot;
  23. android:textSize=&quot;28sp&quot;
  24. android:textStyle=&quot;bold|italic&quot; /&gt;
  25. &lt;View
  26. android:layout_width=&quot;match_parent&quot;
  27. android:layout_height=&quot;1dp&quot;
  28. android:layout_below=&quot;@+id/friendProfileName&quot;
  29. android:layout_marginStart=&quot;-10dp&quot;
  30. android:layout_marginEnd=&quot;-10dp&quot;
  31. android:background=&quot;@android:color/black&quot; /&gt;
  32. &lt;TextView
  33. android:id=&quot;@+id/about&quot;
  34. android:layout_width=&quot;wrap_content&quot;
  35. android:layout_height=&quot;wrap_content&quot;
  36. android:layout_below=&quot;@+id/friendProfileName&quot;
  37. android:layout_marginTop=&quot;10dp&quot;
  38. android:text=&quot;About Me: &quot;
  39. android:textSize=&quot;24sp&quot;
  40. android:textStyle=&quot;bold|italic&quot; /&gt;
  41. &lt;TextView
  42. android:id=&quot;@+id/friendAboutMe&quot;
  43. android:layout_width=&quot;match_parent&quot;
  44. android:layout_height=&quot;wrap_content&quot;
  45. android:layout_below=&quot;@+id/friendProfileName&quot;
  46. android:layout_marginTop=&quot;10dp&quot;
  47. android:layout_marginBottom=&quot;10dp&quot;
  48. android:layout_toEndOf=&quot;@+id/about&quot;
  49. android:text=&quot;@string/status&quot;
  50. android:textSize=&quot;26sp&quot;
  51. android:textStyle=&quot;italic&quot; /&gt;
  52. &lt;View
  53. android:layout_width=&quot;match_parent&quot;
  54. android:layout_height=&quot;1dp&quot;
  55. android:layout_below=&quot;@+id/friendAboutMe&quot;
  56. android:layout_marginStart=&quot;-10dp&quot;
  57. android:layout_marginEnd=&quot;-10dp&quot;
  58. android:background=&quot;@android:color/black&quot; /&gt;
  59. &lt;TextView
  60. android:id=&quot;@+id/phoneNumber&quot;
  61. android:layout_width=&quot;wrap_content&quot;
  62. android:layout_height=&quot;wrap_content&quot;
  63. android:layout_below=&quot;@+id/friendAboutMe&quot;
  64. android:layout_marginTop=&quot;10dp&quot;
  65. android:text=&quot;Phone Number: &quot;
  66. android:textSize=&quot;24sp&quot;
  67. android:textStyle=&quot;bold|italic&quot; /&gt;
  68. &lt;TextView
  69. android:id=&quot;@+id/friendPhoneNumber&quot;
  70. android:layout_width=&quot;match_parent&quot;
  71. android:layout_height=&quot;wrap_content&quot;
  72. android:layout_below=&quot;@+id/friendAboutMe&quot;
  73. android:layout_marginTop=&quot;10dp&quot;
  74. android:layout_toEndOf=&quot;@+id/phoneNumber&quot;
  75. android:text=&quot;@string/fakeNumber&quot;
  76. android:textSize=&quot;26sp&quot;
  77. android:textStyle=&quot;italic&quot; /&gt;
  78. &lt;View
  79. android:layout_width=&quot;match_parent&quot;
  80. android:layout_height=&quot;1dp&quot;
  81. android:layout_below=&quot;@+id/friendPhoneNumber&quot;
  82. android:layout_marginStart=&quot;-10dp&quot;
  83. android:layout_marginTop=&quot;10dp&quot;
  84. android:layout_marginEnd=&quot;-10dp&quot;
  85. android:background=&quot;@android:color/black&quot; /&gt;
  86. &lt;Button
  87. android:id=&quot;@+id/blockUser&quot;
  88. android:layout_width=&quot;wrap_content&quot;
  89. android:layout_height=&quot;wrap_content&quot;
  90. android:layout_alignParentBottom=&quot;true&quot;
  91. android:layout_centerHorizontal=&quot;true&quot;
  92. android:background=&quot;@drawable/button&quot;
  93. android:text=&quot;Block User&quot;
  94. android:textColor=&quot;@android:color/white&quot;
  95. android:textSize=&quot;24sp&quot;
  96. android:textStyle=&quot;italic&quot; /&gt;

</RelativeLayout>

JAVA CODE FriendProfileViewActivity

  1. public class FriendProfileViewActivity extends AppCompatActivity {
  2. TextView name, about, phoneNumber;
  3. Button blockUser;
  4. CircleImageView profilepic;
  5. DatabaseReference databaseReference;
  6. FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
  7. String friendId, friendPhoneNumber;
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_friend_profile_view);
  12. profilepic = findViewById(R.id.friendProfilePic);
  13. name = findViewById(R.id.friendProfileName);
  14. about = findViewById(R.id.friendAboutMe);
  15. phoneNumber = findViewById(R.id.friendPhoneNumber);
  16. blockUser = findViewById(R.id.blockUser);
  17. Intent intent = getIntent();
  18. friendId = intent.getStringExtra(&quot;friendId&quot;);
  19. databaseReference = FirebaseDatabase.getInstance().getReference(&quot;Users&quot;).child(friendId);
  20. databaseReference.addValueEventListener(new ValueEventListener() {
  21. @Override
  22. public void onDataChange(@NonNull DataSnapshot snapshot) {
  23. if (snapshot.exists()) {
  24. Users users = snapshot.getValue(Users.class);
  25. name.setText(users.getUsername());
  26. about.setText(users.getAboutMe());
  27. phoneNumber.setText(users.getFullPhoneNumber());
  28. // Glide.with(FriendProfileViewActivity.this).load(users.getProfilephotoURL())
  29. // .fitCenter().placeholder(R.drawable.ic_user).into(profilepic);
  30. friendPhoneNumber = users.getFullPhoneNumber();
  31. }
  32. }
  33. @Override
  34. public void onCancelled(@NonNull DatabaseError error) {
  35. }
  36. });
  37. blockUser.setOnClickListener(v -&gt; {
  38. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  39. builder.setTitle(&quot;Block User&quot;);
  40. builder.setIcon(R.drawable.ic_block);
  41. builder.setCancelable(false);
  42. builder.setMessage(&quot;Do you wan to block User?&quot;);
  43. builder.setPositiveButton(&quot;Yes&quot;, (dialog, which) -&gt; {
  44. BlockList blockList = new BlockList(friendPhoneNumber);
  45. DatabaseReference blockUser = FirebaseDatabase.getInstance().
  46. getReference(&quot;BlockList&quot;).child(user.getUid());
  47. blockUser.child(friendId).setValue(blockList);
  48. BlockList blockList1 = new BlockList(user.getPhoneNumber());
  49. blockUser = FirebaseDatabase.getInstance().
  50. getReference(&quot;BlockList&quot;).child(friendId);
  51. blockUser.child(user.getUid()).setValue(blockList1);
  52. databaseReference = FirebaseDatabase.getInstance().getReference().child(&quot;ChatsList&quot;)
  53. .child(user.getUid());
  54. databaseReference.child(friendId).removeValue();
  55. DatabaseReference friendChatList = FirebaseDatabase.getInstance().getReference().child(&quot;ChatsList&quot;)
  56. .child(friendId);
  57. friendChatList.child(user.getUid()).removeValue();
  58. Intent intent1 = new Intent(FriendProfileViewActivity.this, MainScreen.class);
  59. startActivity(intent1);
  60. });
  61. builder.setNegativeButton(&quot;Cancel&quot;, (dialog, which) -&gt; dialog.dismiss());
  62. AlertDialog alertDialog = builder.create();
  63. alertDialog.show();
  64. });
  65. }
  66. }

答案1

得分: 1

错误堆栈如下:

  1. 由: android.view.InflateException引发: 二进制XML文件行#8: 二进制XML文件行#8: 膨胀de.hdodenhof.circleimageview.CircleImageView类时出错
  2. 引起: android.view.InflateException: 二进制XML文件行#8: 膨胀de.hdodenhof.circleimageview.CircleImageView类时出错
  3. 引起: java.lang.reflect.InvocationTargetException
  4. java.lang.reflect.Constructor.newInstance0(本机方法)处
  5. java.lang.reflect.Constructor.newInstanceConstructor.java:430)处
  6. android.view.LayoutInflater.createViewLayoutInflater.java:656)处
  7. android.view.LayoutInflater.createViewFromTagLayoutInflater.java:798)处
  8. android.view.LayoutInflater.createViewFromTagLayoutInflater.java:738)处
  9. android.view.LayoutInflater.rInflateLayoutInflater.java:869)处
  10. android.view.LayoutInflater.rInflateChildrenLayoutInflater.java:832)处
  11. android.view.LayoutInflater.inflateLayoutInflater.java:518)处
  12. android.view.LayoutInflater.inflateLayoutInflater.java:426)处
  13. android.view.LayoutInflater.inflateLayoutInflater.java:377)处
  14. androidx.appcompat.app.AppCompatDelegateImpl.setContentViewAppCompatDelegateImpl.java:555)处
  15. androidx.appcompat.app.AppCompatActivity.setContentViewAppCompatActivity.java:161)处
  16. za.co.Test.testit.MainActivity.onCreateMainActivity.java:12)处
  17. android.app.Activity.performCreateActivity.java:6910)处
  18. android.app.Instrumentation.callActivityOnCreateInstrumentation.java:1123)处
  19. android.app.ActivityThread.performLaunchActivityActivityThread.java:2757)处
  20. android.app.ActivityThread.handleLaunchActivityActivityThread.java:2875)处
  21. android.app.ActivityThread.-wrap12ActivityThread.java)处
  22. android.app.ActivityThread$H.handleMessageActivityThread.java:1578)处
  23. android.os.Handler.dispatchMessageHandler.java:105)处
  24. android.os.Looper.loopLooper.java:156)处
  25. android.app.ActivityThread.mainActivityThread.java:6623)处
  26. java.lang.reflect.Method.invoke(本机方法)处
  27. com.android.internal.os.ZygoteInit$MethodAndArgsCaller.runZygoteInit.java:942)处
  28. com.android.internal.os.ZygoteInit.mainZygoteInit.java:832)处
  29. 引起: java.lang.IllegalArgumentException: 不支持ScaleType FIT_CENTER
  30. de.hdodenhof.circleimageview.CircleImageView.setScaleTypeCircleImageView.java:134)处

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

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

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

英文:

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

  1. Caused by: android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class de.hdodenhof.circleimageview.CircleImageView
  2. Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class de.hdodenhof.circleimageview.CircleImageView
  3. Caused by: java.lang.reflect.InvocationTargetException
  4. at java.lang.reflect.Constructor.newInstance0(Native Method)
  5. at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
  6. at android.view.LayoutInflater.createView(LayoutInflater.java:656)
  7. at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:798)
  8. at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:738)
  9. at android.view.LayoutInflater.rInflate(LayoutInflater.java:869)
  10. at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:832)
  11. at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
  12. at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
  13. at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
  14. at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
  15. at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
  16. at za.co.Test.testit.MainActivity.onCreate(MainActivity.java:12)
  17. at android.app.Activity.performCreate(Activity.java:6910)
  18. at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
  19. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2757)
  20. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2875)
  21. at android.app.ActivityThread.-wrap12(ActivityThread.java)
  22. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1578)
  23. at android.os.Handler.dispatchMessage(Handler.java:105)
  24. at android.os.Looper.loop(Looper.java:156)
  25. at android.app.ActivityThread.main(ActivityThread.java:6623)
  26. at java.lang.reflect.Method.invoke(Native Method)
  27. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
  28. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
  29. Caused by: java.lang.IllegalArgumentException: ScaleType FIT_CENTER not supported.
  30. 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:

确定