Reputation: 103
I am trying to create a android app that has one activity and multiple fragments. Each fragment will ocupy the entire screen when it is in view and with a replace transaction it should be switched to another fragment.
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
This is the container for the fragments. The problem is that a fragment that I want to use is a GoogleMap. This is my code for the map fragment:
public class Map extends Fragment{
private final static String TAG = "MAP Fragment";
public GoogleMap googleMap = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setUpMapIfNeeded();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.map_fragment, null);
return view;
}
private void setUpMapIfNeeded()
{
if(googleMap == null)
{
googleMap = ((MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.map)).getMap();
if(googleMap != null)
{
GoogleMapOptions options = new GoogleMapOptions();
options.mapType(GoogleMap.MAP_TYPE_NORMAL)
.camera(new CameraPosition(new LatLng(25f, 47f), 13f, 0f, 0f));
}
else
{
Log.d(TAG, "googleMap is null !!!!!!!!!!!!!!!");
}
}
}
}
and it's layout:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
And in the Main activity:
@Override
protected void onCreate(Bundle savedInstanceState)
{
Map test = new Map();
getSupportFragmentManager().beginTransaction()
.add(R.id.content_frame, test)
.commit();
}
I get a null pointer exception at: (I belive it cannot find the R.id.map)
googleMap = ((MapFragment) getActivity().getFragmentManager()
.findFragmentById(R.id.map)).getMap();
If I don't use "getActivity()." it says that it cannot cast from Fragment to MapFragment.
I need "googleMap" for creating markers on the map.
When I make the same application but without fragments (my main activity diplays the map and i don't have other fragments) everithing works fine.
I have found this similar topics: Google Maps API v2 Custom MapFragment + SimpleFragment and error using maps in fragment . But if I extend FragmentActivity or SupportMapFragment I cannot replace the fragment with another using transactions.
Conclusion: I would like that when an action occurs (click or something) I only have to replace the fragmens like this:
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
What am I doing wrong? Or should I take a different approach to the problem?
(P.S. I am realtively new to android)
Upvotes: 3
Views: 1207
Reputation: 103
The problem is that I was trying to change the map element before it was displayed setUpMapIfNeeded()
is called in onCreate()
(the view is inflated only in onCreateView()
).
If I move the call setUpMapIfNeeded()
in onResume()
everything works because then the view is inflated and it can use the map and modify it.
This is where I found the answer
Upvotes: 1