Я пытаюсь показать GoogleMap в своем приложении, но у меня возникают проблемы с самого начала этой задачи. Линия:getSupportFragmentManager() findFragmentById() всегда возвращает null
SupportMapFragment mapFrag = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
всегда возвращает нуль.
Я пробовал много вещей, но ничего не работает. Не могли бы вы помочь мне найти проблему?
Вот мой код:
public class MapSectionFragment extends Fragment implements
ConnectionCallbacks,
OnConnectionFailedListener,
LocationListener,
OnMyLocationButtonClickListener,
OnMapReadyCallback{
private GoogleMap map;
Location location;
GoogleApiClient mGoogleApiClient;
private static final LocationRequest REQUEST = LocationRequest.create()
.setInterval(5000) // 5 seconds
.setFastestInterval(16) // 16ms = 60fps
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
private void createMapView(){
try{
//here it comes
SupportMapFragment mapFrag = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
map = mapFrag.getMap(); //throws NullPointerException
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_section_map,
container, false);
createMapView();
return rootView;
}
@Override
public void onPause() {
super.onPause();
mGoogleApiClient.disconnect();
}
@Override
public void onResume() {
super.onResume();
mGoogleApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient,
REQUEST,
this); // LocationListener
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
@Override
public void onMapReady(GoogleMap googleMap) {
map.setMyLocationEnabled(true);
}
@Override
public boolean onMyLocationButtonClick() {
Toast.makeText(getActivity(), "MyLocation button clicked", Toast.LENGTH_SHORT).show();
// Return false so that we don't consume the event and the default behavior still occurs
// (the camera animates to the user's current position).
return false;
}
}
А вот XML "fragment_section_map" с МАПИД:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/refresh_map_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="@string/refresh_map" />
<fragment
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>
Спасибо за вашу помощь!
возможного дубликата [findFragmentById всегда возвращает нуль] (http://stackoverflow.com/questions/ 10833666/findfragmentbyid-always-returns-null) – CurlyCorvus
Посмотрите на это http://stackoverflow.com/questions/15762266/findfragmentbyid-always-returns-a-null –
Очевидно, что фрагмент фрагмента не находится внутри менеджера фрагментов activitie. .. очевидно, по крайней мере по двум причинам ... во-первых: это дочерний фрагмент вашего фрагмента ... во-вторых, 'onCreateView' не закончен внутри, ну, хммм,' onCreateView' ... поэтому представление не добавлено к Мероприятия ... поэтому фрагмент карты не добавлен к активности. – Selvin