如何处理 android.view.InflateException?当我尝试打开地图时出现此错误
Posted
技术标签:
【中文标题】如何处理 android.view.InflateException?当我尝试打开地图时出现此错误【英文标题】:How to handle android.view.InflateException? when I tried to open map I got this error 【发布时间】:2021-07-26 16:01:56 【问题描述】:如何解决android.view.InflateException?我将显示我的错误和代码,任何人都可以帮助我解决这个异常。
错误:
android.view.InflateException: Binary XML file line #12 in com.transasia.ctrack:layout/agent_locationmap_layout: Binary XML file line #12 in com.transasia.ctrack:layout/agent_locationmap_layout: Error inflating class androidx.fragment.app.FragmentContainerView
Caused by: android.view.InflateException: Binary XML file line #12 in com.transasia.ctrack:layout/agent_locationmap_layout: Error inflating class androidx.fragment.app.FragmentContainerView
Caused by: java.lang.ClassCastException: com.google.android.gms.location.places.ui.PlaceAutocompleteFragment cannot be cast to androidx.fragment.app.Fragment
at androidx.fragment.app.Fragment.instantiate(Fragment.java:548)
at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java:57)
at androidx.fragment.app.FragmentManager$3.instantiate(FragmentManager.java:390)
at androidx.fragment.app.FragmentContainerView.<init>(FragmentContainerView.java:161)
at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:51)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:356)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:335)
at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1069)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:997)
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.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.transasia.ctrackvp.agentfold.AgentAndHospitalLocationActivity.onCreate(AgentAndHospitalLocationActivity.java:71)
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)
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)
2021-05-04 15:08:01.216 13007-13007/com.transasia.ctrack E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.transasia.ctrack, PID: 13007
java.lang.RuntimeException: Unable to start activity ComponentInfocom.transasia.ctrack/com.transasia.ctrackvp.agentfold.AgentAndHospitalLocationActivity: android.view.InflateException: Binary XML file line #12 in com.transasia.ctrack:layout/agent_locationmap_layout: Binary XML file line #12 in com.transasia.ctrack:layout/agent_locationmap_layout: Error inflating class androidx.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 #12 in com.transasia.ctrack:layout/agent_locationmap_layout: Binary XML file line #12 in com.transasia.ctrack:layout/agent_locationmap_layout: Error inflating class androidx.fragment.app.FragmentContainerView
Caused by: android.view.InflateException: Binary XML file line #12 in com.transasia.ctrack:layout/agent_locationmap_layout: Error inflating class androidx.fragment.app.FragmentContainerView
Caused by: java.lang.ClassCastException: com.google.android.gms.location.places.ui.PlaceAutocompleteFragment cannot be cast to androidx.fragment.app.Fragment
at androidx.fragment.app.Fragment.instantiate(Fragment.java:548)
at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java:57)
at androidx.fragment.app.FragmentManager$3.instantiate(FragmentManager.java:390)
at androidx.fragment.app.FragmentContainerView.<init>(FragmentContainerView.java:161)
at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:51)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:356)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:335)
at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1069)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:997)
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.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.transasia.ctrackvp.agentfold.AgentAndHospitalLocationActivity.onCreate(AgentAndHospitalLocationActivity.java:71)
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)
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_>
<LinearLayout
android:id="@+id/ll_place"
android:layout_
android:layout_
android:orientation="vertical">
<androidx.fragment.app.FragmentContainerView // showing error in this part <<<<<--------
android:id="@+id/place_fragment_map"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
android:layout_
android:layout_ />
</LinearLayout>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/main_content"
android:layout_
android:layout_
android:layout_below="@+id/ll_place">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/mapp"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_
android:layout_
tools:context="com.transasia.ctrackvp.agentfold.AgentAndHospitalLocationActivity" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<Button
android:id="@+id/btn_route"
android:layout_
android:layout_
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="Direction" />
</RelativeLayout>
Java:
@Override
protected void onCreate(Bundle savedInstanceState)
try
super.onCreate(savedInstanceState);
setContentView(R.layout.agent_locationmap_layout); //Error in this part<<<----
catch (Exception e)
Log.e(TAG, "onCreateView", e);
throw e;
告诉我为什么会出现这个异常,我尝试了一些我在 *** 中看到的解决方案,但我仍然没有解决这个错误 ..................................................... ......谢谢!......................................... ..................................................... .....................
【问题讨论】:
向我们展示您的agent_locationmap_layout
你有它,我提到了 XML
【参考方案1】:
尝试使用此代码
android:name="com.google.android.gms.location.places.ui.SupportPlaceAutocompleteFragment" />
和
SupportPlaceAutocompleteFragment placeFragmentMap= (SupportPlaceAutocompleteFragment)
getChildFragmentManager()
.findFragmentById(R.id.place_fragment_map);
而不是这个
<androidx.fragment.app.FragmentContainerView
android:id="@+id/place_fragment_map"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
android:layout_
android:layout_ />
【讨论】:
Bro SupportPlaceAutocompletefragment 已弃用,我没有得到 getchildfragment 如何解决这个问题implementation 'com.google.android.gms:play-services-places:16.0.0'
你在 build.gradle 中使用这个吗?如果是,则使用 16.0.0 更改版本以上是关于如何处理 android.view.InflateException?当我尝试打开地图时出现此错误的主要内容,如果未能解决你的问题,请参考以下文章