Reputation: 4840
UPDATED I read many topics but nothing helped or I didn't understand what to do. I get null and I don't know why. I am just learning so please help me.
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
at com.example.text.fragments.MapFragment.initilizeMap(MapFragment.java:52)
at com.example.text.fragments.MapFragment.onViewCreated(MapFragment.java:46)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:971)
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.executePendingTransactions(FragmentManager.java:488)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
at android.support.v4.view.ViewPager$3.run(ViewPager.java:249)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:792)
at android.view.Choreographer.doCallbacks(Choreographer.java:596)
at android.view.Choreographer.doFrame(Choreographer.java:556)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:778)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
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:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
This is Fragment where I implemented getMap
public class MapFragment extends Fragment implements OnMapReadyCallback{
private GoogleMap googleMap;
/**
* function to load map. If map is not created it will create it for you
* */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.map_fragment, container, false);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
// here, as doc say, the map can be initialized, or the service is not available
initilizeMap();
}
private void initilizeMap() {
if (googleMap == null) {
((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(
R.id.map)).getMapAsync(this);
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getActivity().getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
@Override
public void onMapReady(GoogleMap mGoogleMap) {
googleMap = mGoogleMap;
}
}
This is map_fragment.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"/>
</RelativeLayout>
This is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test" >
<permission
android:name="com.example.test.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.test.permission.MAPS_RECEIVE" />
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="22" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:supportsRtl="true">
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBUeq6ANw9MyK9vwB4phnSOkp6e_UccN4E" />
</application>
</manifest>
I use sliding tabs and in one tab has to have map. I create this map in this part of code in ViewPagerAdapter:
@Override
public Fragment getItem(int position) {
if(mNumbOfTabs == 3) {
if (position == 0) {
A a = new A();
return a;
} else if (position == 1) {
B b = new B();
return b;
} else {
MapFragment mapFragment = new MapFragment();
return mapFragment;
}
}
Upvotes: 2
Views: 5979
Reputation: 2430
Since the 6.5 version of Play Services Google recommends using of asynchronous getting of GoogleMap and getMap()
is now deprecated. As stkent's mentioned you have to call getMapAsync(this)
.
As this method asynchronous you need some callback for this method, that's why also you have to make your fragment implementing OnMapReadyCallback
and in the method onMapReady()
of these callbacks you can get reference to the map.
Also change line ((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);
to ((SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);
'cause inside fragment you have to use ChildFragmentManager.
Upvotes: 5
Reputation: 900
I had the same problem, with google maps v2 you should use getExtendedMap()
instead. Try this.
Upvotes: 2