我正在使用com.google.android.gms:play-services-maps:7.5.0
版Google地图服务。当试图打电话给下面我得到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);
}
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>
您试图在片段存在之前找到它。您指示具有该片段的布局是fragment_example_map.xml
。但是,您试图在膨胀该布局文件之前找到地图片段。这不行。
除此之外,您似乎试图从该片段的onCreateView()
方法中的另一个片段内部获取地图片段。我不知道为什么你在这里嵌套片段,因为它似乎会使你的代码更复杂,更脆弱,没有明显的好处。
我使用的是SupportMapFragment,在XML中我定义了MapFragment name属性
@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);
}
所以我用SupportMapFragment替换了代码,它开始工作正常
android:name="com.google.android.gms.maps.MapFragment"
用这个。
android:name="com.google.android.gms.maps.SupportMapFragment"
替换此代码
GoogleMap gooleMap;
((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap map) {
googleMap = map;
}
和
<fragment
android:name="com.google.android.gms.maps.MapFragment"
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="400dp" />
只需在您的活动中致电if (googleMap == null) {
mapFrag = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
mapFrag.getMapAsync(this);
}else{
GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
}
中的supportMapFragment.getMapAsync(this);
如果你想在侧面Fragment中使用map Fragment,请使用此方法
onResume
在onCreate中尝试此代码
SupportMapFragment mMapFragment = SupportMapFragment.newInstance();
FragmentTransaction fragmentTransaction =
getChildFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.flMap, mMapFragment);
fragmentTransaction.commit();
mMapFragment.getMapAsync(this);
我在带有片段的地图中的评论,首先你应该引用你的视图,因为你会从中调用一些东西,这就是为什么我先把我的视图充气 qazxsw poi
第二次调用子片段管理器View view = inflater.inflate(R.layout.activity_maps, null, false);
而不是this.getChildFragmentManager()
以下是查看地图的完整示例
getActivity().getSupportFragmentManager()
必要的许可
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));
}
}
加上特征元素
<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" />
大多数导入谷歌地图键
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
如果您面对<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="your_key_here" />
,请更新添加此元数据
Error inflating class fragment
清单摘要
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="your_key_here" />
activity_maps.xml
<?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>
使用<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" />
代替片段中的getChildFragmentManager()
。
喜欢:
getActivity().getSupportFragmentManager()
如果您在片段中使用SupportMapFragment mapFragment = (SupportMapFragment)getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
,请使用以下命令更改代码:
android:name="com.google.android.gms.maps.MapFragment"
但是,如果您在片段中使用MapFragment mapFragment1 = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
mapFragment1.getMapAsync(this);
,请使用以下代码:
android:name="com.google.android.gms.maps.SupportMapFragment"
这对我有用:
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
面临同样的问题。由于您在片段中使用地图片段,因此请使用SupportMapFragment mapFragment = (SupportMapFragment)getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
而不是getChildFragmentManager()
或getActivity().getFragmentManager()
。
根据Android版本或SDK版本5及更高版本,使用getChildFragmentManager()及其下方使用getFragmentManager()。
当您在片段内使用map时,getFragmentManager()可能无法正常工作。你必须使用getChildFragmentManager()。
getFragmentManager()
以下是我的xml声明
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.fragment_track_order_map_fragment);
mapFragment.getMapAsync(this);
您还需要在片段中实现OnMapReadyCallback。
简单的答案是,如果你想添加Map Fragment
<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" />
在您的片段中:像这样的布局
<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" />
然后你必须在像这样膨胀片段之后添加这一行
<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>
进入你的Activity:喜欢这个布局
@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);
.....
那么你必须添加这段代码。
<?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>
如果我错了,请纠正我,但在给布局膨胀或设置内容视图后总是添加地图片段。