[英]google map in fragment activity in Android studio
当我将 Google Maps API 放入 Fragment 时,它似乎已经启动,但应用程序停止并关闭。
应用程序关闭前的最后一张图片
日志 Output
2019-11-19 14:18:05.137 19795-24984/? E/chph: *~*~*~ Channel {0} was not shutdown properly!!! ~*~*~*
Make sure to call shutdown()/shutdownNow() and wait until awaitTermination() returns true.
java.lang.RuntimeException: ManagedChannel allocation site
at chpg.<init>(:com.google.android.gms@19629028@19.6.29 (100400-278422107):2)
at chph.<init>(:com.google.android.gms@19629028@19.6.29 (100400-278422107):2)
at chic.b(:com.google.android.gms@19629028@19.6.29 (100400-278422107):8)
at sqq.a(:com.google.android.gms@19629028@19.6.29 (100400-278422107):24)
at sqq.a(:com.google.android.gms@19629028@19.6.29 (100400-278422107):33)
at awdm.a(:com.google.android.gms@19629028@19.6.29 (100400-278422107):81)
at awdm.a(:com.google.android.gms@19629028@19.6.29 (100400-278422107):165)
at awdj.a(:com.google.android.gms@19629028@19.6.29 (100400-278422107):2)
at awdt.a(Unknown Source:2)
at brav.a(:com.google.android.gms@19629028@19.6.29 (100400-278422107):2)
at brcx.b(:com.google.android.gms@19629028@19.6.29 (100400-278422107):2)
at brbw.run(:com.google.android.gms@19629028@19.6.29 (100400-278422107):7)
at brcz.run(:com.google.android.gms@19629028@19.6.29 (100400-278422107):1)
at sxr.b(:com.google.android.gms@19629028@19.6.29 (100400-278422107):15)
at sxr.run(:com.google.android.gms@19629028@19.6.29 (100400-278422107):10)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at tdp.run(Unknown Source:7)
at java.lang.Thread.run(Thread.java:764)
我搜索了一下,从日志 output 中得到另一个错误
2019-11-19 15:08:13.298 4490-4548/com.ourcrewapps.webbil E/AndroidRuntime: FATAL EXCEPTION: Thread-6
Process: com.ourcrewapps.webbil, PID: 4490
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/ProtocolVersion;
at ep.b(:com.google.android.gms.dynamite_mapsdynamite@13280052@13.2.80 (040700-211705629):3)
at eo.a(:com.google.android.gms.dynamite_mapsdynamite@13280052@13.2.80 (040700-211705629):4)
at eq.a(:com.google.android.gms.dynamite_mapsdynamite@13280052@13.2.80 (040700-211705629):55)
at com.google.maps.api.android.lib6.drd.ap.a(:com.google.android.gms.dynamite_mapsdynamite@13280052@13.2.80 (040700-211705629):11)
at dx.a(:com.google.android.gms.dynamite_mapsdynamite@13280052@13.2.80 (040700-211705629):17)
at dx.run(:com.google.android.gms.dynamite_mapsdynamite@13280052@13.2.80 (040700-211705629):65)
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.http.ProtocolVersion" on path: DexPathList[[zip file "/system/priv-app/PrebuiltGmsCore/app_chimera/m/MapsDynamite.apk"],nativeLibraryDirectories=[/data/user_de/0/com.google.android.gms/app_chimera/m/00000006/n/x86, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at ad.loadClass(:com.google.android.gms.dynamite_dynamiteloader@13280052@13.2.80 (040700-211705629):25)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at ep.b(:com.google.android.gms.dynamite_mapsdynamite@13280052@13.2.80 (040700-211705629):3)
at eo.a(:com.google.android.gms.dynamite_mapsdynamite@13280052@13.2.80 (040700-211705629):4)
at eq.a(:com.google.android.gms.dynamite_mapsdynamite@13280052@13.2.80 (040700-211705629):55)
at com.google.maps.api.android.lib6.drd.ap.a(:com.google.android.gms.dynamite_mapsdynamite@13280052@13.2.80 (040700-211705629):11)
at dx.a(:com.google.android.gms.dynamite_mapsdynamite@13280052@13.2.80 (040700-211705629):17)
at dx.run(:com.google.android.gms.dynamite_mapsdynamite@13280052@13.2.80 (040700-211705629):65)
Java Class Output [LocationFragment.java]
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.MapView;
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;
public class LocationFragment extends Fragment implements OnMapReadyCallback {
private MapView mapView;
private GoogleMap gmap;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_location,container,false);
SupportMapFragment mapci =(SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mapci.getMapAsync(this);
return view;
}
@Override
public void onMapReady(GoogleMap googleMap) {
gmap = googleMap;
LatLng lng = new LatLng(19.169259,73.341601);
gmap.addMarker(new MarkerOptions().position(lng).title("sad"));
gmap.moveCamera(CameraUpdateFactory.newLatLng(lng));
}
}
XML Output [fragment_location.xml]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<fragment 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"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
/>
</RelativeLayout>
Api 密钥已添加
In the latest update of Google for Maps, only MapView is supported for fragments.
MapFragment & SupportMapFragment doesn't work.
I might be wrong but this is what I saw after trying to implement MapFragment & SupportMapFragment.
Setting up the layout for showing the map in the xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
While Add below code in your java file:
public class MapViewFragment extends Fragment {
MapView mMapView;
private GoogleMap googleMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.location_fragment, container, false);
mMapView = (MapView) rootView.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume(); // needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap mMap) {
googleMap = mMap;
// For showing a move to my location button
googleMap.setMyLocationEnabled(true);
// For dropping a marker at a point on the Map
LatLng sydney = new LatLng(-34, 151);
googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker Title").snippet("Marker Description"));
// For zooming automatically to the location of the marker
CameraPosition cameraPosition = new CameraPosition.Builder().target(sydney).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
});
return rootView;
}
错误有所改善。 问题是库版本和最小 sdk 错误
最小 sdk
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.ourcrewapps.webbil"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
库版本
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:asynclayoutinflater:28.0.0'
implementation 'com.android.support:exifinterface:28.0.0'
implementation 'com.android.support:animated-vector-drawable:28.0.0'
implementation 'com.android.support:support-media-compat:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.google.firebase:firebase-database:17.0.0'
implementation 'com.google.android.gms:play-services-maps:16.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.