Reputation: 1179
I am trying to use SupportMapFragment and I get this error:
java.lang.NullPointerException: Attempt to invoke interface method 'void com.google.maps.api.android.lib6.c.c.o()' on a null object reference
at com.google.maps.api.android.lib6.c.z.b(Unknown Source)
The error appears somewhere here:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
SupportMapFragment supportMapFragment;
if (Build.VERSION.SDK_INT < 21) {
supportMapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
} else {
supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
}
supportMapFragment.getMapAsync(this);
super.onViewCreated(view, savedInstanceState);
}
I implemented the OnMapReadyCallBack:
public class MapsFragment extends SupportMapFragment implements OnMapReadyCallback {
I don't catch where the null reference is.
The onCreate Function:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_maps, container, false);
}
Stack-Track:
03-22 22:38:07.291 3758-3758/itcc.li.lieventuregradle E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: itcc.li.lieventuregradle, PID: 3758
java.lang.NullPointerException: Attempt to invoke interface method 'void com.google.maps.api.android.lib6.c.c.o()' on a null object reference
at com.google.maps.api.android.lib6.c.z.b(Unknown Source)
at com.google.android.gms.maps.internal.x.onTransact(SourceFile:115)
at android.os.Binder.transact(Binder.java:380)
at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onResume(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment$a.onResume(Unknown Source)
at com.google.android.gms.dynamic.a$7.b(Unknown Source)
at com.google.android.gms.dynamic.a.a(Unknown Source)
at com.google.android.gms.dynamic.a.onResume(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment.onResume(Unknown Source)
at android.support.v4.app.Fragment.performResume(Fragment.java:1829)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:993)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:456)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
fragment_maps.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Upvotes: 0
Views: 1089
Reputation: 38223
Problem 1)
If the following line is really in your code (which is correct)
public class MapsFragment extends SupportMapFragment ...
then remove the onCreateView
method.
You're trying to inflate a map fragment into a map fragment.
Problem 2)
Get rid of this abomination.
if (Build.VERSION.SDK_INT < 21) {
supportMapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
} else {
supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
}
You access the fragment manager from a fragment by calling getFragmentManager()
or getChildFragmentManager()
depending on which you need. Both will be instances of a support fragment manager since you're working in a support fragment.
Also you don't have to find a map fragment, you're it.
Problem 3)
Respect lifecycle logic. Call super.onViewCreated(...)
at the beginning of overridden the method and super.onDestroyView()
at the end of the overridden method (if you will use it). In the same way you want the fragment's ancestors initialize first and clean up last.
Upvotes: 7