英文:
Android - Binary XML file line #10: Error inflating class <unknown>
问题
我正在尝试转到另一个活动,但应用程序崩溃并显示以下错误信息。
我有两个活动:
- 聊天活动(ChatActivity)
- 好友个人资料查看活动(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:
- Acivity Chat
- 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.
<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">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="40dp"
android:layout_height="50dp"
android:layout_marginEnd="10dp"
android:background="@android:color/transparent"
android:src="@drawable/ic_back"
android:scaleType="centerCrop"/>
<RelativeLayout
android:id="@+id/viewProfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/friendpic"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentStart="true"
android:layout_marginStart="-5dp"
android:layout_marginEnd="20dp"
android:src="@drawable/ic_user" />
<TextView
android:id="@+id/friendname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/friendpic"
android:fontFamily="@font/alegreya_sc_italic"
android:text="Username"
android:textSize="24sp" />
<TextView
android:id="@+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/friendname"
android:layout_toEndOf="@+id/friendpic"
android:text="offline"
android:textSize="20sp" />
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
JAVA CODE for Chat
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 code for FriendProfileViewActivity 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">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/friendProfilePic"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginBottom="30dp"
android:scaleType="fitCenter" />
<TextView
android:id="@+id/friendProfileName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/friendProfilePic"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="Friend Name"
android:textSize="28sp"
android:textStyle="bold|italic" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/friendProfileName"
android:layout_marginStart="-10dp"
android:layout_marginEnd="-10dp"
android:background="@android:color/black" />
<TextView
android:id="@+id/about"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/friendProfileName"
android:layout_marginTop="10dp"
android:text="About Me: "
android:textSize="24sp"
android:textStyle="bold|italic" />
<TextView
android:id="@+id/friendAboutMe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/friendProfileName"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_toEndOf="@+id/about"
android:text="@string/status"
android:textSize="26sp"
android:textStyle="italic" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/friendAboutMe"
android:layout_marginStart="-10dp"
android:layout_marginEnd="-10dp"
android:background="@android:color/black" />
<TextView
android:id="@+id/phoneNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/friendAboutMe"
android:layout_marginTop="10dp"
android:text="Phone Number: "
android:textSize="24sp"
android:textStyle="bold|italic" />
<TextView
android:id="@+id/friendPhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/friendAboutMe"
android:layout_marginTop="10dp"
android:layout_toEndOf="@+id/phoneNumber"
android:text="@string/fakeNumber"
android:textSize="26sp"
android:textStyle="italic" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/friendPhoneNumber"
android:layout_marginStart="-10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="-10dp"
android:background="@android:color/black" />
<Button
android:id="@+id/blockUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/button"
android:text="Block User"
android:textColor="@android:color/white"
android:textSize="24sp"
android:textStyle="italic" />
</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("friendId");
databaseReference = FirebaseDatabase.getInstance().getReference("Users").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 -> {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Block User");
builder.setIcon(R.drawable.ic_block);
builder.setCancelable(false);
builder.setMessage("Do you wan to block User?");
builder.setPositiveButton("Yes", (dialog, which) -> {
BlockList blockList = new BlockList(friendPhoneNumber);
DatabaseReference blockUser = FirebaseDatabase.getInstance().
getReference("BlockList").child(user.getUid());
blockUser.child(friendId).setValue(blockList);
BlockList blockList1 = new BlockList(user.getPhoneNumber());
blockUser = FirebaseDatabase.getInstance().
getReference("BlockList").child(friendId);
blockUser.child(user.getUid()).setValue(blockList1);
databaseReference = FirebaseDatabase.getInstance().getReference().child("ChatsList")
.child(user.getUid());
databaseReference.child(friendId).removeValue();
DatabaseReference friendChatList = FirebaseDatabase.getInstance().getReference().child("ChatsList")
.child(friendId);
friendChatList.child(user.getUid()).removeValue();
Intent intent1 = new Intent(FriendProfileViewActivity.this, MainScreen.class);
startActivity(intent1);
});
builder.setNegativeButton("Cancel", (dialog, which) -> 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论