在CoordinatorLayout中,Supportmapfragment不工作。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在CoordinatorLayout中,Supportmapfragment不工作。相关的知识,希望对你有一定的参考价值。

我试图把我的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_width="match_parent"
    android:layout_height="match_parent">

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

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

新濠天地娱乐手机版下载_新濠天地娱乐手机版下载_地图.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 ComponentInfo{com.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.

当运行应用程序时,我得到了像上面提到的错误。我有任何其他可能的解决方案,这个问题。或者为什么会出现这种情况?

答案

我想你在你的应用中使用了androidx,但是你的CoordinatorLayout使用的是android.support.design版本。所以你需要把它改成androidx版本。

从这个。

<android.support.design.widget.CoordinatorLayout...

到这个。

<androidx.coordinatorlayout.widget.CoordinatorLayout...

以上是关于在CoordinatorLayout中,Supportmapfragment不工作。的主要内容,如果未能解决你的问题,请参考以下文章

如何在折叠布局下的 CoordinatorLayout 中正确对齐元素?

什么是 CoordinatorLayout?

显示没有 CoordinatorLayout 的 Snackbar

CoordinatorLayout布局中要注意的问题

如何在 CoordinatorLayout 中禁用 AppBarLayout 的滚动?

CoordinatorLayout原理的简析