使用AppCompat/material主题膨胀类com.google.android.material.appbar.



我真的不知道我到底缺少了什么,但即使使用了Appcomat主题,我也会遇到同样的膨胀异常错误。logcat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aasha/com.example.aasha.MainActivity}: android.view.InflateException: Binary XML file line #23: Binary XML file line #23: Error inflating class com.google.android.material.appbar.AppBarLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3139)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3282)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1970)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7156)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
Caused by: android.view.InflateException: Binary XML file line #23: Binary XML file line #23: Error inflating class com.google.android.material.appbar.AppBarLayout
Caused by: android.view.InflateException: Binary XML file line #23: Error inflating class com.google.android.material.appbar.AppBarLayout

布局:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/chat_good_things">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/message_box">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
app:layout_scrollFlags="scroll|enterAlways">
....
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/chat_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:clipToPadding="false"
app:layout_anchor="@id/toolbar"
app:layout_anchorGravity="bottom"
android:layout_gravity="bottom"/>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<LinearLayout
android:id="@+id/message_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
android:background="@android:color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" >
<ImageButton
android:id="@+id/image_send_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:srcCompat="@drawable/ic_image_add" />
<EditText
android:id="@+id/message_type_box"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_weight="1"
android:layout_gravity="center"
android:focusableInTouchMode="true"
android:textIsSelectable="true"
android:hint="Type a message..."
android:inputType="text" />
<ImageButton
android:id="@+id/message_send_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:srcCompat="@drawable/ic_message_send"/>
</LinearLayout>
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

主题:

<style name="Theme.Aasha" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>

等级:


implementation 'com.google.android.material:material:1.4.0'
// androidx
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
implementation "androidx.recyclerview:recyclerview:1.2.1"
implementation "androidx.navigation:navigation-fragment-ktx:$navigation_version"
implementation "androidx.navigation:navigation-ui-ktx:$navigation_version"
implementation 'de.hdodenhof:circleimageview:3.1.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

我搜索并发现了这个StackOverflow,尝试了所有的解决方案,包括:

无效缓存/重新启动运气不佳重构->迁移到AndroidX,说明未找到项目的用法

我很困惑,用同样的单一布局做了一个单独的项目,运行起来没有任何问题,但我无法区分并在这里找到错误。请帮忙!

不要忘记在style.xml中使用Theme.MaterialComponents.Light.NoActionBar(或任何Theme.MaterialComponents.*.NoActionBar(作为父AppTheme

implementation 'com.google.android.material:material:1.0.0'
classpath 'com.android.tools.build:gradle:3.2.0'

将Gradle和材料设计更改为最新版本

我使用的是来自androidx.core.splashscreen.SplashScreen库的SplashScreenApi。

我知道我必须在MainAcitive((中的setContentView()之前调用installSplashScreen(),但我不知道的是,它甚至应该在任何viewBinding初始化之前调用。对于像我这样缺乏经验的人来说,这几天都没有被注意到。该应用程序运行顺利,在任何材料设计标签实现之前,一切都很好。

之前:

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
installSplashScreen()
setContentView(binding.root)
...
}

之后:

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
installSplashScreen()
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
...
}

我太笨了:(.我花了一段时间,因为logcat总是指向setContentView()

相关内容

最新更新