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();
6
  • Paste your manifest code also please. Commented Jul 29, 2015 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. Commented Jul 29, 2015 at 18:13
  • @AdarshYadav btw I've added the manifest, just in case. Thanks Commented Jul 29, 2015 at 18:14
  • You didn't add <!-- Goolge Maps API Key --> <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="@string/maps.apikey"/>//Your map key here <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/> Inside <apllication> tag. Commented Jul 29, 2015 at 18:16
  • 1
    possible duplicate of Getting java.lang.NullPointerException in MapFragment Commented Jul 29, 2015 at 18:23

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

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;     } 
}); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.