即时应用程序InflateException

3
运行我的即时应用程序时,我遇到了以下的inflateException错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xfzj.instantappdemo/com.xfzj.instantappdemo.feature.MainActivity}: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class android.support.design.widget.AppBarLayout
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                             at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:148)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                          Caused by: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class android.support.design.widget.AppBarLayout
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                                                                             at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
                                                                             at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                             at com.xfzj.instantappdemo.feature.MainActivity.onCreate(MainActivity.java:17)
                                                                             at android.app.Activity.performCreate(Activity.java:6237)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
</pre>

这是我的layout.xml文件:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.xfzj.instantappdemo2.feature.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@android:drawable/ic_dialog_email" />
</LinearLayout>

这是我特性模块的 build.gradle 文件:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation project(':base')
    implementation 'com.android.support:appcompat-v7:25.4.0'
    testImplementation 'junit:junit:4.12'
    implementation 'com.android.support:design:25.4.0'
}

如果我运行已安装的应用程序,它会成功。

com.android.support:design:25.4.0 是否与即时应用程序不兼容?

2个回答

1
"

com.android.support:design:25.4.0支持即时应用程序。问题在于功能模块之间存在appcompatdesign support库的重复依赖。通常,由于这些库在功能上是共同的,它们将被包含在基础功能模块的build.gradle文件中(例如,从Android Studio构建默认的即时应用程序项目):

"
dependencies {
    application project(':app')
    feature project(':feature')
    api 'com.android.support:appcompat-v7:25.4.0'
    api 'com.android.support.constraint:constraint-layout:1.0.2'
    api 'com.android.support:design:25.4.0'
}

由于其他功能模块依赖于基础功能模块,它们不应该自己包含这些库。功能模块的build.gradle只需要包含:

dependencies {
    ...
    implementation project(':base')
    ...
 }

换句话说,基础特性模块中定义的所有库都对其他特性可用,不应作为依赖项重新添加。

0

由于即时应用和安装应用共享设计库,因此存在依赖关系

implementation 'com.android.support:appcompat-v7:25.4.0 

应该在基础功能的build.gradle文件中,而不是在installed文件中。

如果您想将其包含到某些非基础功能中,请确保即时和已安装的build.gradle文件都包含此功能。


它在 feature build.gradle 中,而不是已安装的。 - user6310522
build.gradle 中,即时和已安装的版本都具有特定的依赖关系功能吗? - mol
是的,它们都具有依赖关系。我反编译了即时应用程序的代码,在其中找到了android.support.v7和design等内容。 - user6310522
它解决了问题吗? - Daksh Gargas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接