我有一个Fragment,它包含一个地图,这个地图被加载到回调和OnMapReady上,它成功地在我的位置拉入了一个地图。
然而,我试图添加一个功能,即用户从Spinner中选择一个位置,然后选择在我的地图上绘制相关的多边形,但我一直收到一个错误"试图调用虚拟方法多边形">
我决不是一个有天赋的安卓开发人员,尝试着学习和玩一些东西。
public class FindCarpark extends Fragment {
private SupportMapFragment mapFragment;
private UserLocation.OnFragmentInteractionListener mListener;
Spinner spinCar;
String selectedItem;
GoogleMap mMap;
private static final ArrayList<LatLng> cpB = new ArrayList<>();
public FindCarpark() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_find_carpark, container, false);
Context context = getActivity().getApplicationContext();
spinCar = rootView.findViewById(R.id.car_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this.getActivity(),R.array.car_parks, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinCar.setAdapter(adapter);
spinCar.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (i > 0) {
selectedItem = spinCar.getSelectedItem().toString().trim();
addCoords(selectedItem.toString());
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
if (mapFragment == null) {
mapFragment = SupportMapFragment.newInstance();
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap mMap) {
new googleMap().makeMap(mMap);
}
});
}
getChildFragmentManager().beginTransaction().replace(R.id.map, mapFragment).commit();
return rootView;
}
public void addCoords(String coords){
Log.i("coords", coords);
switch (coords){
case "B":
cpB.add(new LatLng(53.797731, -1.546351));
cpB.add(new LatLng(53.797551, -1.545983));
cpB.add(new LatLng(53.797447, -1.545267));
cpB.add(new LatLng(53.797731, -1.546351));
makePolygon(cpB);
Log.i("coords", cpB.toString());
break;
default:
break;
}
}
public void makePolygon(ArrayList<LatLng> coords){
mMap.addPolygon(new PolygonOptions()
.addAll(coords)
.strokeColor(Color.RED)
.fillColor(Color.BLUE));
}
XML:
<FrameLayout 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=".FindCarpark">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Spinner
android:id="@+id/car_spinner"
android:prompt="@string/app_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">
</Spinner>
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</FrameLayout>
错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Polygon com.google.android.gms.maps.GoogleMap.addPolygon(com.google.android.gms.maps.model.PolygonOptions)' on a null object reference
mMap变量永远不会被填充。这随后会导致一个空指针异常。问题是:
if (mapFragment == null) {
mapFragment = SupportMapFragment.newInstance();
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap mMap) {
new googleMap().makeMap(mMap);
}
});
}
这里mMap是局部变量,而您稍后尝试访问的mMap是对象的属性,因此:
if (mapFragment == null) {
mapFragment = SupportMapFragment.newInstance();
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap mMap) {
FindCarpark.this.mMap = mMap
new googleMap().makeMap(mMap);
}
});
}