无法从 xml 文件膨胀 RelativeLayout
Posted
技术标签:
【中文标题】无法从 xml 文件膨胀 RelativeLayout【英文标题】:Unable to inflate RelativeLayout from xml file 【发布时间】:2020-04-26 21:23:01 【问题描述】:我正在尝试构建一个类似于 BottomSheet 的行为,因此我构建了一个应该从底部滑入的 RelativeLayout。我将它添加到底部的主 xml 文件中,在编辑器中它已经显示,但是当我尝试它时,我的应用程序崩溃并且此错误消息显示:
2020-01-09 11:17:10.841 16474-16474/..app.debug E/androidRuntime: FATAL EXCEPTION: main
Process: ..app.debug, PID: 16474
java.lang.RuntimeException: Unable to start activity ComponentInfo..app.debug/..core.activity.orderbook.OrderbookActivity: android.view.InflateException: Binary XML file line #44: Binary XML file line #44: Error inflating class ...orderbook.bottom_menu.OrderbookFilterMenu
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: android.view.InflateException: Binary XML file line #44: Binary XML file line #44: Error inflating class ...orderbook.bottom_menu.OrderbookFilterMenu
Caused by: android.view.InflateException: Binary XML file line #44: Error inflating class ...module.orderbook.bottom_menu.OrderbookFilterMenu
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
at java.lang.Class.getConstructor0(Class.java:2204)
at java.lang.Class.getConstructor(Class.java:1683)
at android.view.LayoutInflater.createView(LayoutInflater.java:618)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at ...module.orderbook.OrderbookFragment.createView(OrderbookFragment.kt:39)
at ...module_base.fragment.BaseFragment.onCreateView(BaseFragment.java:57)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2439)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273)
at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:620)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)
at android.app.Activity.performStart(Activity.java:6696)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2628)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
这是我的主要 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/orderbook_constraint_layout"
android:orientation="vertical"
android:layout_
android:layout_>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/orderbook_recyclerview"
android:layout_
android:layout_
android:layout_marginBottom="0dp"
android:layout_weight="1"
android:background="@color/background_neutral_primary"
android:overScrollMode="never"
android:scrollbars="none"
app:layout_constraintBottom_toTopOf="@id/recyclerview_anchor"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:visibility="visible" />
<Space
android:id="@+id/recyclerview_anchor"
android:layout_
android:layout_
android:layout_marginTop="14dp"
app:layout_constraintEnd_toEndOf="@+id/orderbook_action_bar"
app:layout_constraintStart_toStartOf="@+id/orderbook_action_bar"
app:layout_constraintTop_toTopOf="@+id/orderbook_action_bar" />
<...ActionBarView
android:id="@+id/orderbook_action_bar"
android:layout_
android:layout_
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:background="@color/dev" />
<...OrderbookFilterMenu
android:id="@+id/filter_menu"
android:layout_
android:layout_
app:layout_constraintTop_toBottomOf="parent"
/>
<...ContextMenu
android:id="@+id/context_menu"
android:layout_
android:layout_
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:visibility="gone"/>
</androidx.constraintlayout.widget.ConstraintLayout>
这是来自我的 Fragment (OrderbookFilterMenu) 的底部菜单
class FilterBottomSheetFragment : BottomSheetDialogFragment()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
return inflater.inflate(R.layout.view_orderbook_bottom_menu_filter, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?)
super.onViewCreated(view, savedInstanceState)
initViews()
setupListeners()
以及我的菜单布局
<?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"
android:orientation="vertical"
android:layout_
android:layout_
android:gravity="center_horizontal"
android:padding="16dp"
android:background="@drawable/bottomsheet_rounded">
<...ConstraintLayout
android:layout_
android:layout_>
<TextView
android:id="@+id/header"
android:layout_
android:layout_
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="@string/order_filter_bottom_menu_header"
android:textColor="@color/text_primary"
android:textStyle="bold"/>
<...FontCheckBox
android:id="@+id/all_orders_checkbox"
android:layout_
android:layout_
android:button="@drawable/checkbox_for_dark_background"
android:checked="false"
android:layout_marginStart="32dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/filter_all_text"
app:layout_constraintBottom_toBottomOf="@id/filter_all_text"/>
<TextView
android:id="@+id/filter_all_text"
android:layout_
android:layout_
android:text="@string/order_filter_all"
android:textColor="@color/text_primary"
android:layout_marginTop="16dp"
app:layout_constraintStart_toEndOf="@+id/all_orders_checkbox"
app:layout_constraintTop_toBottomOf="@+id/header"
android:layout_marginStart="8dp"/>
<...FontCheckBox
android:id="@+id/open_orders_checkbox"
android:layout_
android:layout_
android:button="@drawable/checkbox_for_dark_background"
android:checked="false"
android:layout_marginStart="64dp"
app:layout_constraintTop_toTopOf="@+id/filter_opened_text"
app:layout_constraintBottom_toBottomOf="@id/filter_opened_text"
app:layout_constraintStart_toStartOf="parent"/>
<TextView
android:id="@+id/filter_opened_text"
android:layout_
android:layout_
android:text="@string/order_filter_opened_orders"
android:textColor="@color/text_primary"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/filter_all_text"
app:layout_constraintStart_toEndOf="@+id/open_orders_checkbox"/>
<...FontCheckBox
android:id="@+id/executed_orders_checkbox"
android:layout_
android:layout_
android:button="@drawable/checkbox_for_dark_background"
android:checked="false"
app:layout_constraintStart_toStartOf="@+id/open_orders_checkbox"
app:layout_constraintTop_toTopOf="@+id/filter_executed_text"
app:layout_constraintBottom_toBottomOf="@+id/filter_executed_text"/>
<TextView
android:id="@+id/filter_executed_text"
android:layout_
android:layout_
android:text="@string/order_filter_executed"
android:textColor="@color/text_primary"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
app:layout_constraintStart_toEndOf="@+id/executed_orders_checkbox"
app:layout_constraintTop_toBottomOf="@+id/filter_opened_text"/>
<...FontCheckBox
android:id="@+id/canceled_orders_checkbox"
android:layout_
android:layout_
android:button="@drawable/checkbox_for_dark_background"
android:checked="false"
app:layout_constraintStart_toStartOf="@+id/executed_orders_checkbox"
app:layout_constraintTop_toTopOf="@+id/filter_canceled_text"
app:layout_constraintBottom_toBottomOf="@+id/filter_canceled_text"/>
<TextView
android:id="@+id/filter_canceled_text"
android:layout_
android:layout_
android:text="@string/order_filter_canceled"
android:textColor="@color/text_primary"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
app:layout_constraintStart_toEndOf="@+id/canceled_orders_checkbox"
app:layout_constraintTop_toBottomOf="@+id/filter_executed_text"/>
<...CoreFontButton
android:id="@+id/filter_confirm_button"
style="@style/button_full_primary"
android:layout_marginBottom="20dp"
android:layout_marginTop="16dp"
android:background="@drawable/button_core_rounded_full_primary"
android:text="@string/uebernehmen"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/filter_canceled_text"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
我不明白他为什么不能夸大这种观点。有人有解决方案的想法吗?
【问题讨论】:
请分享完整的错误日志或图片 我添加了完整的错误日志,但出于数据安全考虑对其进行了一些审查 为什么要像<...ActionBarView
这样添加...?
Actionbarview 提供了几个按钮,应该通过点击显示底部菜单。
这是出于数据安全的考虑。这不是我的实际代码的样子。只是审查。
【参考方案1】:
我找到了答案。
我的 OrderbookFilterMenu 有一个这样的构造函数
class OrderbookFilterMenu(context: Context) : RelativeLayout(context)
但显然相对布局需要这样的构造函数
class OrderbookFilterMenu(context: Context, attr: AttributeSet) : RelativeLayout(context, attr)
很抱歉没有提供足够的信息,但感谢您的宝贵时间。
【讨论】:
以上是关于无法从 xml 文件膨胀 RelativeLayout的主要内容,如果未能解决你的问题,请参考以下文章
二进制 XML 文件第 17 行:UnsupportedOperationException 导致的类 <unknown> 膨胀错误:无法转换为维度:type=0x2
有没有办法区分 TextViews 或具有相同 ID 多次从单个 xml 文件膨胀的任何其他视图