我想在片段中显示 Map ,但我总是得到这个NullPointerException:
java.lang.NullPointerException:尝试调用虚方法 ' com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap()'在null 对象参考
我知道在这种情况下有这个例外有很多问题,但我找不到我的错误。我读了我找到的任何答案。
这是我的代码:
public class DetailKarteFragment extends Fragment {
GoogleMap map;
static final LatLng brend = new LatLng(48.079, 8.157);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_detail_karte, null, false);
FragmentTransaction ft = getFragmentManager().beginTransaction();
map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
SupportMapFragment fmap = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
if (fmap == null) {
fmap = SupportMapFragment.newInstance();
ft.add(R.id.map, fmap);
}
ft.commit();
Marker brendMarker = map.addMarker(new MarkerOptions().position(brend).title("Brend"));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(brend, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
return v;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
和xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="@color/background"
android:layout_height="match_parent">
<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="wrap_content" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Route anzeigen"
android:id="@+id/button_route_planen"
android:layout_gravity="center_horizontal"
android:height="48dp"
android:background="@color/secondary_100"
android:layout_marginBottom="-48dp"/>
</LinearLayout>
</LinearLayout>
答案 0 :(得分:0)
在map = (...).getMap()
之后移动ft.commit()
行。您收到该异常是因为FragmentManager
无法找到尚未add
或replace
进入容器的片段。
我认为更好的解决方案是:
SupportMapFragment fmap;
fmap = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
if (fmap == null) {
fmap = SupportMapFragment.newInstance();
ft.add(R.id.map, fmap);
ft.commit();
}
map = fmap.getMap();
答案 1 :(得分:0)
在检查SupportMapFragment
是否存在之前,您正在检索getMap()
对象图。我假设尚未添加此片段,并且您在空引用上调用{{1}}方法。
答案 2 :(得分:0)
我知道这个问题很老......但我刚遇到这个问题,我想分享一下我发生的事情和解决方案。
我试图将MapFragment放在像你一样的片段中。但我忘记把钥匙放在我的清单中了:
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/google_maps_key" />
因此,当在Activity中发生这种情况时,它会抛出异常。这很棒,因为您的应用程序崩溃了,您可以阅读日志并了解您必须做的事情!你收到这条消息:
Caused by: java.lang.RuntimeException: API key not found.
Check that <meta-data android:name="com.google.android.geo.API_KEY" android:value="your API key"/>
is in the <application> element of AndroidManifest.xml
很容易找到需要这样做的事情,对吗?
但是当MapFragment在另一个片段中时它不会发生!
它只返回一个空片段......就像你现在遇到的问题一样。
所以只需把钥匙放在Manifest中就可以了!我希望谷歌解决这个问题,因为我花了几个小时来找出问题所在。