设法使代码无错误,但是在启动时,我总是在行 mMap = mapFrag.getMap(); 处得到一个空指针异常;
为什么会这样?我错过了一些进口或一些步骤吗?我不确定是 SupportMapFragment 还是 GoogleMap 对象导致了问题。
package com.fragments;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
public class MapFragment extends SherlockMapFragment {
private GoogleMap mMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = super.onCreateView(inflater, container, savedInstanceState);
SupportMapFragment mapFrag= (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_map);
mMap = mapFrag.getMap(); //null pointer is here
return root;
}
}
编辑:这是基于此问题here中给出的解决方案的实施的一部分