简体   繁体   English

膨胀 class android.fragment.app.FragmentContainerView 时出错,有人可以告诉我我缺少什么

[英]Error inflating class android.fragment.app.FragmentContainerView, can someone tell me what I'm missing

I'm working on getting the Google Place API to appear within a CardView.我正在努力让 Google Place API 出现在 CardView 中。

The particular code I'm looking at is from GitHub, seems to be working for everyone else but I don't understand why it's throwing me an error.我正在查看的特定代码来自 GitHub,似乎适用于其他所有人,但我不明白为什么它会抛出错误。

This is the error I am getting:这是我得到的错误:

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

The main takeaway is that there is an Error inflating class android.fragment.app.FragmentContainerView and that the issue is at line 50 of the XML.主要的收获是存在错误膨胀 class android.fragment.app.FragmentContainerView 并且问题出在 XML 的第 50 行。

This is the XML:这是 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>

So I guess there is something wrong with the FragmentContainerView?所以我猜 FragmentContainerView 有问题吗? But i'm not getting any errors in the code and nothing is coming up on Google.但我在代码中没有发现任何错误,谷歌也没有任何结果。

I feel like maybe something is outdated rather than outright incorrect but I haven't a clue.我觉得也许有些东西已经过时而不是完全不正确,但我不知道。

Thanks for any input.感谢您的任何输入。

just try to define your startDestination in your navigation xml !只需尝试在导航 xml 中定义您的 startDestination !

You should implement new androidx.fragment dependencies to use FragmentContainerView .您应该实现新的androidx.fragment依赖项以使用FragmentContainerView It is already added since version 1.2.0.自 1.2.0 版起已添加。 You can check here你可以在这里查看

implementation "androidx.fragment:fragment:1.2.5"

In my case, I had a Model which was used as arguments but I had not implemented Serializable and Parcelable implementaion.就我而言,我有一个 Model 用作 arguments 但我没有实现 Serializable 和 Parcelable 实现。

Probably after Refactor->Remove unused resources you can lost id from your androidx.fragment.app.FragmentContainerView - bring it back可能在Refactor->Remove 未使用的资源之后,您可能会从androidx.fragment.app.FragmentContainerView中丢失id - 将其取回

If it is Due to Proguard Then add few lines如果是由于 Proguard 那就加几行

In proguard-rules.pro file在 proguard-rules.pro 文件中

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

This problem was created for me as well.这个问题也是为我创造的。 The reason was two mistakes.原因是两个错误。

1.I did not add startDestination in nav graph. 1.我没有在导航图中添加startDestination。

2.I did not add @AndroidEntryPoint annotation in MainActivitey. 2.我没有在MainActivitey中添加@AndroidEntryPoint注解。

For me it was a typo within one of the fragments the navGraph , in the class path of the android:name attribute.. Adding a wrong path for the fragment class doesn't provide a compile time error, instead it raises android.view.InflateException...Error inflating class android.fragment.app.FragmentContainerView For me it was a typo within one of the fragments the navGraph , in the class path of the android:name attribute.. Adding a wrong path for the fragment class doesn't provide a compile time error, instead it raises android.view.InflateException...Error inflating class android.fragment.app.FragmentContainerView

So, make sure to have a correct packaging path for your fragments in android:name因此,请确保在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>

Your activity must be a child of FragmentActivity to use androidx.fragment.app.FragmentContainerView either you extend the FragmentActivity or the AppCompatActivity which is a child FragmentActivity class.您的活动必须是 FragmentActivity 的子项才能使用 androidx.fragment.app.FragmentContainerView 或者您扩展 FragmentActivity 或作为子项 FragmentActivity class 的 AppCompatActivity。 It is recommended to extend the AppCompatActivity.建议扩展 AppCompatActivity。

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

This error will only occur after you have added progaurd & R8 to your project.只有在您将 progaurd 和 R8 添加到您的项目后,才会出现此错误。 so in progaurd-rule.pro add following two lines:-所以在 progaurd-rule.pro 添加以下两行:-

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

In my case, despite the same error, reason was different.就我而言,尽管存在相同的错误,但原因却有所不同。 However this info about adding some rules to proguard-rules.pro pointed me into the right direction.然而,这个关于向 proguard-rules.pro 添加一些规则的信息为我指明了正确的方向。 This, and some other clues in error log about problems with argument in navigation XML file.这以及错误日志中有关导航 XML 文件中的参数问题的其他一些线索。 Eventually I found that it was problem with using my own classes in argument.最终我发现在参数中使用我自己的类是有问题的。 And I don't mean the class of argument, but the fact that this class also used some of my other classes.而且我不是说 class 的论点,而是这个 class 也使用了我的其他一些类。 And it was enough to add rules for each of these other classes:为每个其他类添加规则就足够了:

-keepnames class <MyClass>
...

If you have done all the solutions and still the problem stands maybe you should check if your fragment constructor is empty.如果您已经完成了所有解决方案,但问题仍然存在,那么您应该检查fragment constructor是否为空。

In my case I looked deeper inside my stacktrace and found this line: Caused by: java.lang.IllegalStateException: FragmentContainerView must have an android:id to add Fragment MyFragment .在我的例子中,我更深入地查看了堆栈跟踪并发现了这一行: Caused by: java.lang.IllegalStateException: FragmentContainerView must have an android:id to add Fragment MyFragment Giving it an id and specifying tools:layout fixed problem给它一个 id 并指定tools:layout固定问题

Error might be caused by the name used in the fragment.错误可能是由片段中使用的名称引起的。 Correct it and you are good to go.纠正它,你对 go 很好。

change改变

android:name="com.google.android.gms.maps.SupportMapFragment"

to

com.rizwanqureshi.kevstaxiscanterbury/ *FragmentPath  /  *FragmentName 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM