所以我有一个包含地图片段的空片段.每当我尝试激活包含地图的片段时,我的应用程序都会崩溃并在此行上返回空指针错误:
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
完整错误消息:
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.MapFragment.getMap()' on a null object reference
at com.collusion.serviceassistant.ReturnVisitDetails.onViewCreated(ReturnVisitDetails.java:39)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:904)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:684)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
这是我的片段代码:
import android.app.Activity;
import android.app.Dialog;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
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.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class ReturnVisitDetails extends Fragment {
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(53.551, 9.993);
private GoogleMap map;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_return_visit_add, container, false);
return rootView;
}
public void onViewCreated(View view, Bundle savedInstanceState)
{
// Get a handle to the Map Fragment
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
.title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
.position(KIEL)
.title("Kiel")
.snippet("Kiel is cool")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
}
还有我的父片段XML布局:
<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"
tools:context="com.collusion.serviceassistant.ReturnVisitDetails"
android:background="#009688">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>
有人知道为什么地图片段返回null吗?
编辑:所以现在我有以下代码来初始化地图:
MapFragment mapFragment = new MapFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.rl_map_container, mapFragment).commit();
map = mapFragment.getMap();
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
.title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
.position(KIEL)
.title("Kiel")
.snippet("Kiel is cool")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
它仍然返回空指针值
- 2021-1-111 #
- 2021-1-112 #
我已经解决了这个问题。 当我有一个viewpager时,此行:
mMap = ((SupportMapFragment) MainActivity.fragmentManager .findFragmentById(R.id.mapView)).getMap();
您必须更改为此:
mMap = ((SupportMapFragment) getChildFragmentManager() .findFragmentById(R.id.mapView)).getMap();
- 2021-1-113 #
您的Class程从
Fragment
扩展 ,而您使用的是Fragment
在布局中声明,但嵌套片段仅在动态添加时起作用。因此,您有两个选择:
1)从
FragmentAcivity
扩展 代替Fragment
并采用适当的方法.2)添加
Fragment
动态,如下所示:// ReturnVisitDetails
MapFragment mapFragment = new MapFragment(); FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); transaction.add(R.id.rl_map_container, mapFragment).commit();
//布局
<FrameLayout android:id="@+id/rl_map_container" android:layout_width="match_parent" android:layout_height="match_parent" />
--UPDATED--
在
Fragment
内使用Google Maps的完整示例 :// TestFragmentActivity
public class TestFragmentActivity extends android.support.v4.app.FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_fragment_activity); } }
// TestFragment
public class TestFragment extends android.support.v4.app.Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_fragment, container, false); CustomMapFragment mapFragment = new CustomMapFragment(); FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); transaction.add(R.id.map_container, mapFragment).commit(); return rootView; } }
// CustomMapFragment
public class CustomMapFragment extends com.google.android.gms.maps.SupportMapFragment { private final LatLng HAMBURG = new LatLng(53.558, 9.927); @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); GoogleMap googleMap = getMap(); googleMap.addMarker(new MarkerOptions().position(HAMBURG).title("Hamburg")); googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15)); googleMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null); } }
// test_fragment_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:id="@+id/map" class="your_package.TestFragment" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
// test_fragment.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/map_container" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout>
- 2021-1-114 #
我在此搜索了很多主题,因为我在使用旧版Google Play服务时遇到了同样的问题.正如Victor所说,您的班级是从片段开始的,它必须使用布局中定义的另一个片段,所以最好的方法是使用
getChildFragmentManager()
代替getSupportFragmentManager()
也
getMap() method is deprecated
现在,如google docs中所述.请参考更多: https://developers.google.com/android/reference/com/google/android/gms/maps/MapFragment.html#getMap()下面是我在较早的播放服务的各种设备上测试过的工作代码.如果设备具有较旧的Google Play服务,并且对象为
GoogleMap
,则这将生成默认消息 返回null:class MyFragment extends Fragment { SupportMapFragment myMapFragment; GoogleMap myMap; View convertView; FragmentManager fm; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { convertView=inflater.inflate(R.layout.mymap,null); fm=getChildFragmentManager(); myMapFragment=(SupportMapFragment) fm.findFragmentById(R.id.myMapId); // here instead of using getMap(), we are using getMapAsync() which returns a callback and shows map only when it gets ready. //it automatically checks for null pointer or older play services version, getMap() is deprecated as mentioned in google docs. myMapFragment.getMapAsync(new OnMapReadyCallback() { @Override public void onMapReady(GoogleMap googlemap) { // TODO Auto-generated method stub myMap=googlemap; startMap(); } }); return convertView; } }
有关更多信息,请参见此线程。
- 2021-1-115 #
这是否是迁移到目标sdk版本21的问题? 如果是这样,您可能会感兴趣以下解决方案:
MapFragment或MapView getMap()在棒棒糖上返回null
相关问题
- android:如何在Fragment内的webView中添加"返回"功能?androidandroidfragmentsandroidwebview2021-01-11 23:26
- android:如何从FragmentActivity获取片段实例?androiduserinterfaceandroidfragments2021-01-11 19:28
- android:如何在Google Map API v2中处理地图的onTouch事件?androidgooglemapsandroidmapviewgooglemapsandroidapi22021-01-11 18:57
- google maps:Android:如何捕获GPS位置图像androidgooglemapsgps2021-01-11 01:28
- Android Google Maps,如何使每个Marker Infowindow打开不同的Activity?androidgooglemaps2021-01-10 13:57
当我尝试将Eclipse项目移植到Android Studio项目时,发生了此问题。
我将其与Fragment Activity一起使用,如下所示:
但是,问题是,我之前使用的是特定版本的Google Play服务(4.3.23)。
但是Gradle构建会选择最新版本(
compile 'com.google.android.gms:play-services:+')
我尝试了其他一些特定版本,例如(6.1.71).但是仍然给出了空值异常。
我使用了与以前相同的旧版本,并且可以正常工作。
这对我来说是一个临时解决方案.但是,当我需要更新到 新的播放服务库。
似乎没有特定版本的地图库可用于Gradle构建 当我这样尝试时: