0
MapFragment.java无法加载地图成的FrameLayout
public class MapFragment extends SupportMapFragment implements OnMapReadyCallback{
private GoogleMap googleMap;
private Context context;
public MapFragment(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
View root = inflater.inflate(R.layout.item_individual_warehouse, null, false);
onMapReady(googleMap);
return root;
}
private void initializeMap(){
if(googleMap == null) {
SupportMapFragment fm =(SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map);
fm.getMapAsync(this);
if(googleMap == null){
Toast.makeText(context,"Sorry! We are unable to load maps", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onMapReady(GoogleMap gm) {
googleMap = gm;
initializeMap();
}
}
以我onCreate方法在RetrieveIndividualWarehouseSales.java
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.item_individual_warehouse);
new RetrieveItem().execute();
MapFragment mf = new MapFragment();
mf.onMapReady(googleMap);
}
item_individual_warehouse.xml
<FrameLayout
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/sales_location"/>
错误是:
Caused by: 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
我试图实例化RetrieveIndividualWarehouseSales.java的MapFragment对象,以便我可以显示item_individual_warehouse.xml地图包含了一堆textviews和一个FrameLayout里这是为地图。
确实没有使Fragment的对象在它自己的片段中加载映射 –