我正在尝试在我的某个应用中添加底页。我知道如何使用它们,我在遵循中等教程(并阅读人们在Stack Overflow中遇到的问题)的同时实现了一个,这很好。
但是当我尝试在我的项目中复制它时,无论我将它放在主Activity还是Fragment中,它都会崩溃。即使我从我工作的例子中复制了相同的xmls和程序,它也让我疯狂,因为它崩溃了。
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- Toolbar -->
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="@color/colorPrimary"
app:popupTheme="@style/PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<!-- Fragment -->
<FrameLayout
android:id="@+id/main_screen_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"/>
<!-- Bottom navigation view -->
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemBackground="@color/colorPrimary"
app:itemIconTint="@color/colorGreyLight"
app:itemTextColor="@color/colorGreyLight"
android:layout_gravity="bottom"
app:menu="@menu/bottom_menu" />
frameLayout填充了我的App的多个片段,如下所示:
private fun showNewFragment(fragmentToShow: Fragment, addToBackStack: Boolean = true) {
try {
if (activity.supportFragmentManager != null) {
activity.supportFragmentManager.popBackStack(null,
FragmentManager.POP_BACK_STACK_INCLUSIVE)
val fragmentManager = activity.supportFragmentManager
val transaction = fragmentManager.beginTransaction()
transaction.setTransition(FragmentTransaction.TRANSIT_NONE)
transaction.replace(fragmentPlaceholder, fragmentToShow)
if (addToBackStack) {
transaction.addToBackStack(null)
}
transaction.commit()
fragmentManager.executePendingTransactions()
}
} catch (illegalState: IllegalStateException) {
illegalState.printStackTrace()
}
}
这是我试图放在framelayout中的片段的布局, fragment_lesson_view.xml :
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.trabajofindegrado.ui.screens.lessonview.LessonViewFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_message"
app:layout_anchor="@+id/bottom_sheet_filters"
app:layout_anchorGravity="top|end" />
<include
layout="@layout/bottom_sheet_filters"
/>
我认为问题可能出在这里,因为FloatingActionButton没有正确锚定布局,就像找不到它一样。
片段,只是空的测试它:
class LessonViewFragment : Fragment() {
companion object {
val TAG = LessonViewFragment::class.java.simpleName
val startIntent: Intent
get() = Intent(TAG)
}
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? =
inflater.inflate(R.layout.fragment_lesson_view, container, false)
}
最后,这里是 bottom_sheet_filters.xml ,我不认为它有问题,因为它是从我做的例子中100%复制的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="340dp"
android:background="@android:color/darker_gray"
android:orientation="vertical"
app:behavior_hideable="true"
app:behavior_peekHeight="@dimen/peek_height"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<TextView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="@dimen/peek_height"
android:background="?android:attr/windowBackground"
android:text="@string/sheet_title" />
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/very_large_test"
android:textColor="@android:color/white" />
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
它也不是一个问题,因为它在片段内部,我尝试在示例中使用片段并且它工作,然后我尝试将BottomSheet移动到活动并且它一直崩溃。日志:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.trabajofindegrado/com.trabajofindegrado.activities.application.ApplicationActivity}: android.view.InflateException: Binary XML file line #32: Could not inflate Behavior subclass android.support.design.widget.BottomSheetBehavior
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 #32: Could not inflate Behavior subclass android.support.design.widget.BottomSheetBehavior
Caused by: java.lang.RuntimeException: Could not inflate Behavior subclass android.support.design.widget.BottomSheetBehavior
at android.support.design.widget.CoordinatorLayout.parseBehavior(CoordinatorLayout.java:628)
at android.support.design.widget.CoordinatorLayout$LayoutParams.<init>(CoordinatorLayout.java:2795)
at android.support.design.widget.CoordinatorLayout.generateLayoutParams(CoordinatorLayout.java:1703)
at android.support.design.widget.CoordinatorLayout.generateLayoutParams(CoordinatorLayout.java:112)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:989)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:854)
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 android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.trabajofindegrado.activities.application.ApplicationActivity.onCreate(ApplicationActivity.kt:50)
at android.app.Activity.performCreate(Activity.java:6720)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
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: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.support.design.widget.CoordinatorLayout.parseBehavior(CoordinatorLayout.java:626)
at android.support.design.widget.CoordinatorLayout$LayoutParams.<init>(CoordinatorLayout.java:2795)
at android.support.design.widget.CoordinatorLayout.generateLayoutParams(CoordinatorLayout.java:1703)
at android.support.design.widget.CoordinatorLayout.generateLayoutParams(CoordinatorLayout.java:112)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:989)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:854)
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 android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.trabajofindegrado.activities.application.ApplicationActivity.onCreate(ApplicationActivity.kt:50)
at android.app.Activity.performCreate(Activity.java:6720)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
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: java.lang.UnsupportedOperationException: Can't convert value at index 1 to dimension: type=0x4
at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:720)
at android.support.design.widget.BottomSheetBehavior.<init>(BottomSheetBehavior.java:187)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.support.design.widget.CoordinatorLayout.parseBehavior(CoordinatorLayout.java:626)
at android.support.design.widget.CoordinatorLayout$LayoutParams.<init>(CoordinatorLayout.java:2795)
at android.support.design.widget.CoordinatorLayout.generateLayoutParams(CoordinatorLayout.java:1703)
at android.support.design.widget.CoordinatorLayout.generateLayoutParams(CoordinatorLayout.java:112)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:989)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:854)
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 android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.trabajofindegrado.activities.application.ApplicationActivity.onCreate(ApplicationActivity.kt:50)
at android.app.Activity.performCreate(Activity.java:6720)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
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)
提前致谢
答案 0 :(得分:1)
无法发表评论&gt;声誉点不足。
我的2美分:
不完全确定,但不应该所有设计支持库小部件都是CoordinatorLayout
的直接子项吗?
确保所有其他尺寸都正确(具有正确的单位,与其他尺寸一致,例如peek_height
不大于底部纸张本身)。
另外,你正在嵌套CoordinatorLayout
,我怀疑你有最好的性能数字。我建议你找一个更好的设计。
我希望这能回答你的问题