android getActivity().getSupportFragmentManager().findFragmentById is null - android

I'm trying to load a map with com.google.android.gms.maps.SupportMapFragment, but I get null in findFragmentById.
My xml fragment file:
<fragment
android:id="#+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
class="com.google.android.gms.maps.SupportMapFragment" />
MainActivity.java:
public class MainActivity extends ActionBarActivity{
private void selectItem(int position){
//some code
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, new MyMapFragment).commit();
}
}
MapFragment:
MyMapFragment extends android.support.v4.app.Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
SupportMapFragment smf = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
//here smf is null
}
}

This is the answer:
Note: You cannot inflate a layout into a fragment when that layout includes a fragment. Nested fragments are only supported when added to a fragment dynamically.
And furthermore when playing with nested fragments, use getChildFragmentManager().

Related

NullPointerException with SupportMapFragment inside a Fragment

I am trying to get a Google Map inside of a Fragment, but I'm getting a NullPointerException. I have read other answers for similar questions but they haven't helped me.
HomeFragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager()
.findFragmentById(R.id.ftMap);
mapFragment.getMapAsync(this);
return view;
}
fragment_home.xml:
<fragment
android:id="#+id/ftMap"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_centerInParent="true"
android:layout_margin="8dp" />
Since you are using a SupportMapFragment nested inside of an outer Fragment, you need to use getChildFragmentManager() instead of getFragmentManager():
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.ftMap);

Naviagation Drawer Stuck with Fragments Injection

I am trying to add to the navigation drawer using fragments as follows using the onNavigationDrawerItemSelected But the compiler is giving me a incomparable type I do not no how I am new to Andriod I used the NaviagationDrwaer Template app to give me heads start.
I am hopeing someone can point me in the right direction with this been driving me nuts. Also any ideas with this apporach how one would handle icons in the menu ?.
public void onNavigationDrawerItemSelected(int position) {
Fragment fragment;
switch(position){
case 0:
fragment = new HomeFragment(); --- This line is where the error comes
break;
}
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager()
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
My HomeFragment Class Conists Of
public class HomeFragment extends Fragment {
View rootview;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.home_layout, container, false);
return rootview;
}
}
And my home_layout file as follows
<RelativeLayout 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="com.example.davidbuckley.cautioapp.home">
<!-- TODO: Update blank fragment layout -->
<TextView android:layout_width="match_parent" android:layout_height="match_parent"
android:text="#string/hello_blank_fragment" />
</RelativeLayout>
One of your fragments is probably a 'support' Fragment: http://developer.android.com/reference/android/support/v4/app/Fragment.html
While the other is probably a native Fragment: http://developer.android.com/reference/android/app/Fragment.html

Inconvertible type error from fragment to SupportMapFragment

So I have a fragment class:
public class MyMapFragmentActivity extends Fragment implements LocationListener,
GoogleMap.OnMapClickListener, GoogleMap.OnMapLongClickListener {
Within it I have a SupportMapFragment defined:
private SupportMapFragment fragment;
I am trying to use the findFragmentById:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getChildFragmentManager();
fragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
if (fragment == null) {
fragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.content_frame, fragment).commit();
}
}
The line
fragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
Despite the 'fragment' being a SupportMapFragment, this is giving inconvertible type error:
cannot cast android.app.fragment to com.google.android.gms.maps.SupportMapFragment !
I'm using fragment layout:
<?xml version="1.0" encoding="utf-8"?>
<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.SupportMapFragment" />
If you use compatibility library - use getSupportFragmentManager().findFragmentById(R.id.fragment); otherwise extend from android.app.Fragment instead of android.support.v4.app.fragment.
You're mixing support fragments and the non-support fragment manager. Use the SupportFragmentManager instead.

Android: GetMap() == null inside DialogFragment

Im trying put a MapFragment inside a DialogFragment, it works fine but i dont know how can I wait until getMap()!=null... hereĀ“s my code:
public class DialogMapa extends DialogFragment{
private SupportMapFragment fragment;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_dialog_mapa, container,false);
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
SupportMapFragment fragment = new SupportMapFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.map, fragment).commit();
GoogleMap mapa = null;
try {
mapa =fragment.getMap();
} catch (Exception e) {
e.printStackTrace();
}
if (mapa==null) Toast.makeText(getActivity(), "NULL", Toast.LENGTH_SHORT).show();
return view;
}
}
and my inflate layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp" >
<FrameLayout
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</RelativeLayout>
Thanks in advance!!
So with the new maps framework, that call is not guaranteed to return non null. The map is pretty complex and is initializing a lot of state on and off the render thread.
In your case it's safe to call after onFragmentsResumed() or after the fragment is attached to an activity. You could always use a handler to check again some point later.
getMap() is not null inside onStart() of the dialog fragment, after view created.

Android Fragment Google Maps V2: Error inflating class

I'm developing an app which uses fragment tab, one of my fragment uses Google Maps V2 "SupportMapFragment"
public class A extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = null;
view = inflater.inflate(R.layout.all_coffee_shops_view, container, false);
map = ((SupportMapFragment) getActivity().getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
}
}
my xml:
<RelativeLayout
android:id="#+id/map_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
android:id="#+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.SupportMapFragment"
/>
</RelativeLayout>
I'm calling B Fragment from this A fragment(A->B) and when I come back from B->, A fragment onCreateView throws an exception, "android.view.InflateException: Binary XML file line #132: Error inflating class fragment"
when once map fragment is created and when comes back to this fragment again fragment is onCreateView() is called that's why you are getting force close
Try this once in your fragment containing map
#Override
public void onDestroyView() {
super.onDestroyView();
Fragment f = getFragmentManager().findFragmentById(R.id.map);
if (f != null)
getFragmentManager().beginTransaction().remove(f).commit();
}
please comment me about the result
public void onDestroyView() {
super.onDestroyView();
Log.d(TAG, "onDestroyView");
Fragment f = getActivity()
.getSupportFragmentManager().findFragmentById(R.id.map);
if (f != null) {
getActivity().getSupportFragmentManager()
.beginTransaction().remove(f).commit();
}
}

Categories

Resources