我需要像这样下拉片段来添加Google地图片段。
当用户选择下拉应用程序需要找到位置时。我试图在android studio中使用与MapsActivity相同的代码。我得到java.lang.NullPointerException
这个代码I' m在片段中使用
public class MapFragment extends Fragment implements OnMapReadyCallback {
private GoogleMap mMap;
public MapFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_map, container, false);
SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager()
.findFragmentById(R.id.googlemap);
mapFragment.getMapAsync(this);
return view;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(7, 81);
mMap.addMarker(new MarkerOptions().position(sydney).title("Sri Lanka"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
in .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="arimac.dss.lk.dsi.fragments.MapFragment">
<!-- TODO: Update blank fragment layout -->
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/googlemap"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="arimac.dss.lk.dsi.MapsActivity" />
如何添加下拉菜单和Google地图