Ich verwende die com.google.Android.gms:play-services-maps:7.5.0
-Version von Google Maps-Diensten. Wenn ich versuche, das Folgende anzurufen, bekomme ich Java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.Android.gms.maps.SupportMapFragment.getMapAsync(com.google.Android.gms.maps.OnMapReadyCallback)' on a null object reference
.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this); //error
return inflater.inflate(R.layout.fragment_example_map, container, false);
}
datei "fragment_example_map.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="app.ExampleMap">
<fragment xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:name="com.google.Android.gms.maps.SupportMapFragment"
Android:id="@+id/map"
Android:layout_width="match_parent"
Android:layout_height="match_parent"/>
</FrameLayout>
Sie versuchen, ein Fragment zu finden, bevor es existiert. Sie geben an, dass das Layout, das das Fragment enthält, fragment_example_map.xml
ist. Sie versuchen jedoch, das Kartenfragment zu finden, bevor Sie die Layoutdatei aufblasen. Das wird nicht funktionieren.
Darüber hinaus scheint es, als würden Sie versuchen, das Kartenfragment aus einem anderen Fragment heraus in die onCreateView()
-Methode dieses Fragments zu bekommen. Ich weiß nicht, warum Sie hier Fragmente verschachteln, da es den Anschein hat, dass Ihr Code komplexer und fragiler wird, ohne einen offensichtlichen Vorteil zu haben.
Mein Kommentar in Karten mit Fragment, zuerst solltest du deine Ansicht auffrischen, da du etwas davon nennst, deshalb blase ich meine Ansicht zuerst aufView view = inflater.inflate(R.layout.activity_maps, null, false);
Zweiter Aufruf des Child Fragment Managers this.getChildFragmentManager()
anstelle von getActivity().getSupportFragmentManager()
Hier ist das vollständige Beispiel zum Anzeigen der Karte
import Android.os.Bundle;
import Android.support.annotation.Nullable;
import Android.support.v4.app.Fragment;
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.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 ClinicFragment extends Fragment implements OnMapReadyCallback {
private GoogleMap mMap;
public static ClinicFragment newInstance() {
ClinicFragment fragment = new ClinicFragment();
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_maps, null, false);
SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return view;
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@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));
}
}
erforderliche Erlaubnis
<permission
Android:name="your.package.name.permission.MAPS_RECEIVE"
Android:protectionLevel="signature" />
<uses-permission Android:name="your.package.name.permission.MAPS_RECEIVE"/>
<uses-permission Android:name="Android.permission.INTERNET" />
<uses-permission Android:name="Android.permission.ACCESS_NETWORK_STATE" />
<uses-permission Android:name="Android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission Android:name="com.google.Android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission Android:name="Android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission Android:name="Android.permission.ACCESS_FINE_LOCATION" />
plus-Feature-Element
<uses-feature
Android:glEsVersion="0x00020000"
Android:required="true"/>
die meisten importieren Google Maps-Schlüssel
<meta-data
Android:name="com.google.Android.maps.v2.API_KEY"
Android:value="your_key_here" />
Aktualisiere füge diese Metadaten hinzu, wenn du auf Fehler beim Aufblasen des Klassenfragments stößt
<meta-data
Android:name="com.google.Android.geo.API_KEY"
Android:value="your_key_here" />
sommerlich zum Mainfest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:Android="http://schemas.Android.com/apk/res/Android"
package="your.package.name" >
// permission here
<application
Android:name=".Activities.MyApplication"
Android:allowBackup="true"
Android:icon="@mipmap/ic_launcher"
Android:label="@string/app_name"
Android:theme="@style/AppTheme" >
// feature element
// maps key
</application>
</manifest>
activity_maps.xml
<fragment xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:map="http://schemas.Android.com/apk/res-auto"
xmlns:tools="http://schemas.Android.com/tools"
Android:id="@+id/map"
Android:name="com.google.Android.gms.maps.SupportMapFragment"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
tools:context="com.imaadv.leaynik.ClinicFragment" />
verwenden Sie getChildFragmentManager()
anstelle von getActivity().getSupportFragmentManager()
im Fragment.
mögen:
SupportMapFragment mapFragment = (SupportMapFragment)getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
Wenn Sie Android:name="com.google.Android.gms.maps.MapFragment"
in Ihrem Fragment verwenden, ändern Sie Ihren Code mit:
MapFragment mapFragment1 = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
mapFragment1.getMapAsync(this);
Wenn Sie jedoch Android:name="com.google.Android.gms.maps.SupportMapFragment"
in Ihrem Fragment verwenden, verwenden Sie diesen Code:
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
das funktioniert für mich:
SupportMapFragment mapFragment = (SupportMapFragment)getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
Verwenden Sie abhängig von der Android-Version oder SDK-Version 5 und höher getChildFragmentManager () und darunter GetFragmentManager ().
Das gleiche Problem konfrontiert. Da Sie das Map-Fragment in einem Fragment verwenden, verwenden Sie getChildFragmentManager () statt getActivity ().
benutze das.
GoogleMap gooleMap;
((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap map) {
googleMap = map;
}
Die einfache Antwort ist, wenn Sie Map Fragment dies hinzufügen möchten
<fragment
xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
Android:id="@+id/map"
Android:name="com.google.Android.gms.maps.SupportMapFragment"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
tools:layout="@layout/fragment_home" />
In dein Fragment: wie dieses Layout
<Android.support.constraint.ConstraintLayout
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:layout_width="match_parent"
Android:layout_height="match_parent"
tools:context="com.example.Android.earthreport.fragments.HomeFragment"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="81dp">
<fragment
xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
Android:id="@+id/map"
Android:name="com.google.Android.gms.maps.SupportMapFragment"
Android:layout_width="0dp"
Android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="@id/guideline4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/guideline3"
tools:layout="@layout/fragment_home" />
</Android.support.constraint.ConstraintLayout>
Dann müssen Sie diese Zeile nach dem Aufblasen des Fragments wie folgt hinzufügen
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Find a reference to the {@link ListView} in the layout
// Inflate the layout for this fragment
// To get the referance we don't have findviewbyId
// method in fragment so we use view
View view = inflater.inflate(R.layout.fragment_home, container, false);
SupportMapFragment mapFragment = (SupportMapFragment)
this.getChildFragmentManager()
.findFragmentById(R.id.map)
mapFragment.getMapAsync(this);
.....
In deine Aktivität: wie dieses Layout
<?xml version="1.0" encoding="utf-8"?>
<Android.support.constraint.ConstraintLayout
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: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:id="@+id/map"
Android:name="com.google.Android.gms.maps.SupportMapFragment"
Android:layout_width="0dp"
Android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:context="package com.example.Android.map" />
</Android.support.constraint.ConstraintLayout>
dann müssen Sie dieses Stück Code hinzufügen.
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_activity);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
Korrigieren Sie mich, wenn ich falsch bin, fügen Sie jedoch nach dem Aufblasen des Layouts immer ein Kartenfragment hinzu oder setzen Sie die Inhaltsansicht.
getFragmentManager () funktioniert möglicherweise nicht, wenn Sie Map innerhalb von Fragment verwenden. Sie müssen getChildFragmentManager () verwenden.
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.fragment_track_order_map_fragment);
mapFragment.getMapAsync(this);
und unten ist meine XML-Deklaration
<fragment
Android:id="@+id/fragment_track_order_map_fragment"
Android:name="com.google.Android.gms.maps.SupportMapFragment"
Android:layout_width="match_parent"
Android:layout_height="match_parent" />
außerdem müssen Sie OnMapReadyCallback in Ihrem Fragment implementieren.
Wenn Sie Map-Fragment in Side-Fragment verwenden möchten, verwenden Sie diese Option
SupportMapFragment mMapFragment = SupportMapFragment.newInstance();
FragmentTransaction fragmentTransaction =
getChildFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.flMap, mMapFragment);
fragmentTransaction.commit();
mMapFragment.getMapAsync(this);
Ich habe SupportMapFragment verwendet und in XML habe ich MapFragment name-Attribut definiert
Android:name="com.google.Android.gms.maps.MapFragment"
also habe ich den Code durch SupportMapFragment ersetzt und es hat gut funktioniert
Android:name="com.google.Android.gms.maps.SupportMapFragment"
Rufen Sie einfach supportMapFragment.getMapAsync(this);
in onResume
in Ihrer Aktivität auf
ersetzen Sie diesen Code
<fragment
Android:name="com.google.Android.gms.maps.MapFragment"
Android:id="@+id/map"
Android:layout_width="fill_parent"
Android:layout_height="400dp" />
und
if (googleMap == null) {
mapFrag = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
mapFrag.getMapAsync(this);
}else{
GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
}
Versuchen Sie diesen Code in onCreate
if (mapFragment != null) {
mapFragment.getMapAsync(this);
}