1

Here's my my_location.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true"
    android:orientation="vertical">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/etName" />

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

Here's my code:

public class MyLocation extends Fragment {
private GoogleMap googleMap;
Context mContext;
public MyLocation (){ mContext = getActivity(); };
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View root = inflater.inflate(R.layout.my_location, container, false);
googleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
}
}

Unfortunately, it displays NPE at the line

googleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

What did I do wrong?

In case, parts of my manifest

<permission
        android:name="com.imincode.meniti.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="com.imincode.meniti.maps.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Solved my problem with the link given in the accepted answer! Here's what I did:

I just changed

googleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

to

googleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
imin
  • 4,504
  • 13
  • 56
  • 103
  • Paste your manifest code also please. – Adarsh Yadav Jul 29 '15 at 18:10
  • @AdarshYadav I don't think there's any problem with my permission or setting inside the manifest file, since the map would display without any problem if remove the line googleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); But by doing so, I can't do pretty much anything with the map. – imin Jul 29 '15 at 18:13
  • @AdarshYadav btw I've added the manifest, just in case. Thanks – imin Jul 29 '15 at 18:14
  • You didn't add //Your map key here Inside tag. – Adarsh Yadav Jul 29 '15 at 18:16
  • Have you tried with getSupportFragmentManager? – jakubbialkowski Jul 29 '15 at 18:18
  • 1
    possible duplicate of [Getting java.lang.NullPointerException in MapFragment](http://stackoverflow.com/questions/31670386/getting-java-lang-nullpointerexception-in-mapfragment) – justHooman Jul 29 '15 at 18:23

2 Answers2

1

Your issue is same with link1 link2 link3
Please do some search before ask a question.

Community
  • 1
  • 1
justHooman
  • 3,044
  • 2
  • 17
  • 15
0

The issue is because it takes time for google map to load ( if you're using inside a fragment, you can expect this load time to be huge) and when you try to access map before it is being loaded, you'll get NullPointerException saying map reference went null. You should wait for the map to load first and then you should access the map views. You have pre-defined callback for that and here is how you should use it.

supportMapFragment.getMapAsync(new OnMapReadyCallback() { 

    @Override 
    public void onMapReady(GoogleMap map) {

        googleMap = map;     } 
}); 
Adarsh Yadav
  • 3,752
  • 3
  • 24
  • 46