0

My question is not a new one. But I still couldn't solve the problem. I imported my project from Eclipse to Android Studio. Originally ,I used google play service 4.3.23 and after the project was imported to Android Studio, it changed to 4+. And MapFragment reply null reference. I tried to follow this and still have the problem.

Any help would be greatly appreciated .Here is my logcat .

10-08 18:01:43.897  30197-30197/sg.searchhouse.mobile.activity I/projectPO﹕ Grange Infinite
10-08 18:01:43.907  30197-30197/sg.searchhouse.mobile.activity E/ViewRootImpl﹕ sendUserActionEvent() mView == null
10-08 18:01:43.907  30197-30197/sg.searchhouse.mobile.activity V/ActivityThread﹕ updateVisibility : ActivityRecord{26a796c6 token=android.os.BinderProxy@2bdc65d {sg.searchhouse.mobile.activity/sg.searchhouse.mobile.activity.NPMProjectSelectionActivity}} show : false
10-08 18:01:44.068  30197-30197/sg.searchhouse.mobile.activity I/u﹕ Making Creator dynamically
10-08 18:01:44.068  30197-30197/sg.searchhouse.mobile.activity D/ResourcesManager﹕ creating new AssetManager and set to /data/app/com.google.android.gms-2/base.apk
10-08 18:01:44.078  30197-30197/sg.searchhouse.mobile.activity W/ResourcesManager﹕ Asset path '/system/framework/com.android.media.remotedisplay.jar' does not exist or contains no resources.
10-08 18:01:44.078  30197-30197/sg.searchhouse.mobile.activity W/ResourcesManager﹕ Asset path '/system/framework/com.android.location.provider.jar' does not exist or contains no resources.
10-08 18:01:44.128  30197-30218/sg.searchhouse.mobile.activity W/art﹕ Suspending all threads took: 16.143ms
10-08 18:01:44.238  30197-30197/sg.searchhouse.mobile.activity D/ChimeraCfgMgr﹕ Loading module com.google.android.gms.maps from APK /data/data/com.google.android.gms/app_chimera/chimera-module-root/module-588df5a5ceb113dbbd16a6b4eefe2d0baa24f6a7/MapsModule.apk
10-08 18:01:44.238  30197-30197/sg.searchhouse.mobile.activity D/ChimeraModuleLdr﹕ Loading module APK /data/data/com.google.android.gms/app_chimera/chimera-module-root/module-588df5a5ceb113dbbd16a6b4eefe2d0baa24f6a7/MapsModule.apk
10-08 18:01:44.258  30197-30197/sg.searchhouse.mobile.activity D/ChimeraFileApk﹕ Primary ABI of requesting process is armeabi-v7a
10-08 18:01:44.258  30197-30197/sg.searchhouse.mobile.activity I/art﹕ DexFile_isDexOptNeeded failed to open oat file '/data/dalvik-cache/arm/data@data@com.google.android.gms@app_chimera@chimera-module-root@module-588df5a5ceb113dbbd16a6b4eefe2d0baa24f6a7@MapsModule.apk@classes.dex' for file location '/data/data/com.google.android.gms/app_chimera/chimera-module-root/module-588df5a5ceb113dbbd16a6b4eefe2d0baa24f6a7/MapsModule.apk': Failed to open oat filename for reading: No such file or directory
10-08 18:01:44.258  30197-30197/sg.searchhouse.mobile.activity D/ChimeraFileApk﹕ Classloading successful. Optimized code found.
10-08 18:01:44.388  30197-30197/sg.searchhouse.mobile.activity I/Google Maps Android API﹕ Google Play services client version: 4452000
10-08 18:01:44.408  30197-30197/sg.searchhouse.mobile.activity I/Google Maps Android API﹕ Google Play services package version: 8115238
10-08 18:01:44.748  30197-30197/sg.searchhouse.mobile.activity D/AbsListView﹕ Get MotionRecognitionManager
10-08 18:01:45.299  30197-30197/sg.searchhouse.mobile.activity D/AbsListView﹕ Get MotionRecognitionManager
10-08 18:01:45.339  30197-30197/sg.searchhouse.mobile.activity I/project data﹕ Grange Infinite
10-08 18:01:45.359  30197-30197/sg.searchhouse.mobile.activity I/System.out﹕ setUpGoogleMap null
10-08 18:01:45.359  30197-30197/sg.searchhouse.mobile.activity D/AndroidRuntime﹕ Shutting down VM
10-08 18:01:45.359  30197-30274/sg.searchhouse.mobile.activity D/State  ::﹕ Start getting bitmap drawable
10-08 18:01:45.359  30197-30275/sg.searchhouse.mobile.activity D/State  ::﹕ Start getting bitmap drawable
10-08 18:01:45.359  30197-30197/sg.searchhouse.mobile.activity W/System.err﹕ java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.MapFragment.getMap()' on a null object reference

Java Code

private GoogleMap map;
if (map == null) {
            map = ((MapFragment) getFragmentManager().findFragmentById(R.id.mapFragment)).getMap();
            // Check if we were successful in obtaining the map.
            if (map != null) {
                zoomToMap();
            }
        }

XML Code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relativelayout_maps"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:visibility="gone"
    >

    <TextView
        android:id="@+id/textView_mapBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:background="@drawable/bluebtn"
        android:gravity="center"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:text="@string/back"
        android:layout_margin="15dp"
        android:textColor="@color/black"
        />

    <fragment
        android:id="@+id/mapFragment"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/textView_mapBack" />

</RelativeLayout>
Community
  • 1
  • 1
meazuri
  • 73
  • 1
  • 11
  • hey, please attach your java code, that will be helpful :-) – penta Oct 08 '15 at 14:28
  • @penta , I just posted my code.Thank you. – meazuri Oct 08 '15 at 14:43
  • use this line and tell me what do you get ? map = ((SupportMapFragment) getActivity().getFragmentManager().findFragmentById(R.id.mapFragment)).getMap(); instead of map = ((MapFragment) getFragmentManager().findFragmentById(R.id.mapFragment)).getMap(); – penta Oct 08 '15 at 14:52

2 Answers2

0

This issue :

java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.MapFragment.getMap()' on a null object reference

could be fixed using SupportMapFragment instead of MapFragment

map = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.mapFragment)).getMap();
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
0

You need to use this:

SupportMapFragment fragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap);
fragment.getMapAsync(new OnMapReadyCallback() {
    @Override
    public void onMapReady(GoogleMap googleMap) {
        map = googleMap; // here you set your Google map

        // here you do the rest of your calculations with your map
    }
});
instanceof
  • 1,404
  • 23
  • 30