Supportmapfragment 在 CoordinatorLayout 中不起作用
Posted
技术标签:
【中文标题】Supportmapfragment 在 CoordinatorLayout 中不起作用【英文标题】:Supportmapfragment not working inside CoordinatorLayout 【发布时间】:2020-08-02 14:47:14 【问题描述】:我试图将我的 GoogleMaps 片段放在 CoordinatorLayout 中,这给了我错误。以前 xml 只是有 GoogleMaps 片段,当时它工作得很好。
activity_bottomsheet_novigo_maps.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_
android:layout_>
<fragment
android:id="@+id/map"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_
android:layout_ />
</android.support.design.widget.CoordinatorLayout>
bottomsheet_novigo_maps.java
package com.novigo.fiori.trackerapp;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
public class bottomsheet_novigo_maps extends AppCompatActivity implements OnMapReadyCallback
private BottomSheetBehavior bottomSheetBehavior;
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bottomsheet_novigo_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
@Override
public void onMapReady(GoogleMap googleMap)
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.novigo.fiori.trackerapp, PID: 28471
java.lang.RuntimeException: Unable to start activity ComponentInfocom.novigo.fiori.trackerapp/com.novigo.fiori.trackerapp.bottomsheet_novigo_maps: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class android.support.design.widget.CoordinatorLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class android.support.design.widget.CoordinatorLayout
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.design.widget.CoordinatorLayout
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.design.widget.CoordinatorLayout" on path: DexPathList[[zip file "/data/app/com.novigo.fiori.trackerapp-1/base.apk"],nativeLibraryDirectories=[/data/app/com.novigo.fiori.trackerapp-1/lib/x86, /data/app/com.novigo.fiori.trackerapp-1/base.apk!/lib/x86, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.view.LayoutInflater.createView(LayoutInflater.java:609)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.novigo.fiori.trackerapp.bottomsheet_novigo_maps.onCreate(bottomsheet_novigo_maps.java:28)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Process 28471 terminated.
在运行应用程序时,我遇到了上面提到的错误。我有任何其他可能的解决方案来解决这个问题。或者为什么会这样?
【问题讨论】:
与 google MapView 一起使用请检查这个***.com/a/43337454/7846071 @NensiKasundra 嗨,我已经用错误/stack_trace 更新了问题。正如你所说,我已经更改了代码。 XML 中有错误 请检查日志本身是否显示原因:android.view.InflateException:二进制 XML 文件第 2 行:二进制 XML 文件第 2 行:错误膨胀类 android.support.design.widget.CoordinatorLayout 引起: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.design.widget.CoordinatorLayout 而不是这个 【参考方案1】:我认为您在您的应用程序中使用了 androidx,但您在 CoordinatorLayout 中使用了 android.support.design 版本。所以需要改成androidx版本
从此:
<android.support.design.widget.CoordinatorLayout...
到这里:
<androidx.coordinatorlayout.widget.CoordinatorLayout...
【讨论】:
以上是关于Supportmapfragment 在 CoordinatorLayout 中不起作用的主要内容,如果未能解决你的问题,请参考以下文章
SupportMapFragment 的 findFragmentById 在 Android Studio 中返回 null
Supportmapfragment 在 CoordinatorLayout 中不起作用
使用 SupportMapFragment 时,我如何知道地图已准备好使用?