【问题标题】:Error inflating class android.fragment.app.FragmentContainerView, can someone tell me what I'm missing膨胀类android.fragment.app.FragmentContainerView时出错,有人可以告诉我我缺少什么
【发布时间】:2020-09-11 20:50:16
【问题描述】:

我正在努力让 Google Place API 出现在 CardView 中。

我正在查看的特定代码来自 GitHub,似乎对其他人都有效,但我不明白为什么它会给我一个错误。

这是我得到的错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.rizwanqureshi.kevstaxiscanterbury, PID: 6818
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rizwanqureshi.kevstaxiscanterbury/com.rizwanqureshi.kevstaxiscanterbury.CustomerMapsActivity}: android.view.InflateException: Binary XML file line #50 in com.rizwanqureshi.kevstaxiscanterbury:layout/activity_customer_maps: Binary XML file line #50 in com.rizwanqureshi.kevstaxiscanterbury:layout/activity_customer_maps: Error inflating class android.fragment.app.FragmentContainerView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: android.view.InflateException: Binary XML file line #50 in com.rizwanqureshi.kevstaxiscanterbury:layout/activity_customer_maps: Binary XML file line #50 in com.rizwanqureshi.kevstaxiscanterbury:layout/activity_customer_maps: Error inflating class android.fragment.app.FragmentContainerView
     Caused by: android.view.InflateException: Binary XML file line #50 in com.rizwanqureshi.kevstaxiscanterbury:layout/activity_customer_maps: Error inflating class android.fragment.app.FragmentContainerView
     Caused by: java.lang.ClassNotFoundException: android.fragment.app.FragmentContainerView
        at java.lang.Class.classForName(Native Method)
        at java.lang.Class.forName(Class.java:454)
        at android.view.LayoutInflater.createView(LayoutInflater.java:815)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1006)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1126)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1126)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
        at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:438)
        at android.app.Activity.setContentView(Activity.java:3324)
        at com.rizwanqureshi.kevstaxiscanterbury.CustomerMapsActivity.onCreate(CustomerMapsActivity.java:116)
        at android.app.Activity.performCreate(Activity.java:7802)
        at android.app.Activity.performCreate(Activity.java:7791)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "android.fragment.app.FragmentContainerView" on path: DexPathList[[zip file "/data/app/com.rizwanqureshi.kevstaxiscanterbury-Nkg-1mQCfbE8I1MoJKHQuA==/base.apk"],nativeLibraryDirectories=[/data/app/com.rizwanqureshi.kevstaxiscanterbury-Nkg-1mQCfbE8I1MoJKHQuA==/lib/x86, /system/lib, /system/product/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
            ... 32 more

主要的结论是存在一个错误膨胀类 android.fragment.app.FragmentContainerView 并且问题出现在 XML 的第 50 行。

这是 XML:

<FrameLayout
    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"
    tools:context=".CustomerLogin" >


    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/buttons">
            <Button
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:id="@+id/logout"
                android:text="logout"
                tools:ignore="ButtonStyle" />
            <Button
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:id="@+id/history"
                android:text="history"
                tools:ignore="ButtonStyle" />
            <Button
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:id="@+id/settings"
                android:text="Settings"
                tools:ignore="ButtonStyle" />
        </LinearLayout>

        <androidx.cardview.widget.CardView
            android:layout_below="@+id/buttons"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="20sp">



//***LINE 50 WHERE THE ERROR IS***



            <android.fragment.app.FragmentContainerView
                android:id="@+id/place_autocomplete_fragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:ignore="ExtraText">
                android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
            </android.fragment.app.FragmentContainerView>
        </androidx.cardview.widget.CardView>
    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="bottom">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/driverInfo"
            android:layout_gravity="bottom"
            android:orientation="horizontal"
            android:background="@android:color/white"
            android:visibility="gone">
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingLeft="40sp">
            </LinearLayout>
        </LinearLayout>
        <LinearLayout
            android:background="@android:color/white"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </LinearLayout>
    </LinearLayout>
</FrameLayout>

所以我猜 FragmentContainerView 有问题?但我在代码中没有收到任何错误,而且 Google 上也没有出现任何内容。

我觉得有些东西可能已经过时而不是完全不正确,但我不知道。

感谢您的任何意见。

【问题讨论】:

  • 这能回答你的问题吗:- stackoverflow.com/questions/19874882/…
  • 尝试将您的 android:name 更改为 class 参考此stackoverflow.com/a/19952291/7806581
  • ClassNotFoundException: android.fragment.app.FragmentContainerView - 您在该布局标签中有错字。您在androidx 中缺少x&lt;androidx.fragment.app.FragmentContainerView。你在第一个标签中是正确的。
  • @riizwaan 你能解决这个问题吗?遇到同样的错误任何见解都会有所帮助
  • 您找到该错误的答案了吗?我遇到了和你一样的问题

标签: java android android-fragments google-places-api


【解决方案1】:

只需尝试在导航 xml 中定义您的 startDestination !

【讨论】:

  • 谢谢,在我的情况下,我设置了不同的路径供参考
  • 这应该是正确答案,对我来说startDestination id 无效。
  • 谢谢。多么神秘的错误信息。如果我没有找到你的答案,我就不知道它与导航组件有关。
  • 是的,你是对的。我很高兴它有帮助
  • 我的startDestination 中有错字。改正后
【解决方案2】:

您应该实现新的androidx.fragment 依赖项以使用FragmentContainerView。自 1.2.0 版起已添加。你可以查看here

implementation "androidx.fragment:fragment:1.2.5"

【讨论】:

  • 对于 Kotlin:implementation 'androidx.fragment:fragment-ktx:1.2.5'.
  • 为什么会这样?您会认为 Fragments 是标准 Android 库的重要组成部分。
【解决方案3】:

在我的例子中,我有一个用作参数的模型,但我没有实现 Serializable 和 Parcelable 实现。

【讨论】:

    【解决方案4】:

    只有在您将 ProGuard 和 R8 添加到您的项目后才会出现此错误。要修复它,请在proguard-rule.pro 中添加以下两行:

    -keepnames class androidx.navigation.fragment.NavHostFragment
    -keep class * extends androidx.fragment.app.Fragment{}
    

    【讨论】:

    • 这对我有用。谢谢!
    • 发布时出现此错误,因此可以正常工作
    【解决方案5】:

    对我来说,这是navGraph 属性的类路径中navGraph 中的一个拼写错误。为片段类添加错误路径不会提供编译时错误,而是提高android.view.InflateException...Error inflating class android.fragment.app.FragmentContainerView

    因此,请确保在android:name 中为您的片段提供正确的打包路径

    <?xml version="1.0" encoding="utf-8"?>
    <navigation 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/nav_graph.xml"
        app:startDestination="@id/mainFragment">
    
        <fragment
            android:id="@+id/mainFragment"
            android:name="com.example.android....MainFragment"  // <<<< This must be a correct path 
            android:label="main_fragment"
        </fragment>
    
    </navigation>
    

    【讨论】:

      【解决方案6】:

      您的活动必须是 FragmentActivity 的子级才能使用 androidx.fragment.app.FragmentContainerView 或者扩展 FragmentActivity 或作为子级 FragmentActivity 类的 AppCompatActivity。建议扩展 AppCompatActivity。

      见:https://www.tutorialguruji.com/android/trying-to-use-navhostfragment-and-im-getting-an-error-error-inflating-class-androidx-fragment-app-fragmentcontainerview/

      【讨论】:

      • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
      【解决方案7】:

      可能在 Refactor->删除未使用的资源之后,您可能会从 androidx.fragment.app.FragmentContainerView 中丢失 id - 将其恢复

      【讨论】:

        【解决方案8】:

        如果是由于Proguard 那就加几行

        在 proguard-rules.pro 文件中

        ....
        # for FragmentContainerView error
        -keepnames class androidx.navigation.fragment.NavHostFragment
        -keep class * extends androidx.fragment.app.Fragment{}
        

        【讨论】:

          【解决方案9】:

          这个问题也是为我造成的。原因是两个错误。

          1.我没有在导航图中添加startDestination。

          2.我没有在MainActivitey中添加@AndroidEntryPoint注解。

          【讨论】:

            猜你喜欢
            • 2013-02-20
            • 1970-01-01
            • 2021-11-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-12-20
            • 2020-12-31
            • 2011-12-18
            相关资源
            最近更新 更多