Java 无法修复片段NullPointerException中的映射片段

Java 无法修复片段NullPointerException中的映射片段,java,android,android-fragments,nullpointerexception,Java,Android,Android Fragments,Nullpointerexception,我试图在片段中显示一个映射,但我总是得到以下NullPointerException: java.lang.NullPointerException:尝试调用虚拟方法 'com.google.android.gms.maps.GoogleMap 空值上的com.google.android.gms.maps.SupportMapFragment.getMap() 对象引用 我知道除了这个例外还有很多问题,但我找不到我的错误。我读了我找到的任何答案 这是我的密码: public class Det

我试图在片段中显示一个映射,但我总是得到以下NullPointerException:

java.lang.NullPointerException:尝试调用虚拟方法 'com.google.android.gms.maps.GoogleMap 空值上的com.google.android.gms.maps.SupportMapFragment.getMap() 对象引用

我知道除了这个例外还有很多问题,但我找不到我的错误。我读了我找到的任何答案

这是我的密码:

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>

只需在
ft.commit()之后移动
map=(…).getMap()
行即可。您获得该异常是因为
FragmentManager
找不到尚未
add
ed或
replace
d到容器中的片段

我认为更好的解决办法是:

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();

在检查
SupportMapFragment
是否存在之前,您正在检索
GoogleMap
对象映射。我假设这个片段还没有被添加,您正在对空引用调用
getMap()
方法

我知道这个问题很老了。。。但我刚刚遇到了这个问题,我想和大家分享一下发生在我身上的事情和解决办法

我试着把一个MapFragment放在一个片段里面,就像你一样。但我忘了把钥匙放在我的舱单上:

<meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="@string/google_maps_key" />

因此,当这在活动中发生时,它会抛出一个异常。这太棒了,因为你的应用程序崩溃了,你可以阅读日志,了解你要做什么!您将收到以下消息:

    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
原因:java.lang.RuntimeException:未找到API密钥。
检查一下
位于AndroidManifest.xml的元素中
用这种方法很容易找到需要做的事情,对吗

但是当MapFragment在另一个片段中时,它不会发生

它只返回一个空片段。。。就像你现在遇到的问题一样


所以只要把钥匙放在舱单上,你就没事了!我希望谷歌能解决这个问题,因为我花了几个小时才找到问题所在。

fmap肯定是空的,但在我的例子中,我们正在检查空值,所以不会出现错误。更重要的是,为了实现这一点,您应该将布局中的
更改为
,并删除其
android:name
属性。还可以尝试使用
getChildFragmentManager()
更改
getFragmentManager()
,因为我们在片段中使用片段。我尝试了您所说的一切,但没有任何效果。我的地图总是空的。。。我也尝试了不同的组合。。。有没有其他方法可以将MapFragment放入片段中?