Gabriel
Gabriel

Reputation: 229

Implementing google map API android

I want to implement google map API on android studio, first time i use the template given, then generate api_key. It's working!

Then I want to implement this into my project, my project using navigation drawer template, which load fragments. so i create new fragment to load this map.

when i run the app, i got error and suddenly shutdown the app. here was the error:

E/dalvikvm: Could not find class 'com.google.android.chimera.Activity', referenced from method mu.b

I'm not sure why, and how to fix it.

Here my snippets:

Load fragment on my MainActivity.java

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.fragmentPlaceholder, new GoogleMapFragment());
        ft.commit();

GoogleMapFragment.java

public class GoogleMapFragment extends Fragment implements OnMapReadyCallback {

private static final String LOG = "Location";
DatabaseHelper db;
private GoogleMap mMap;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    // Defines the xml file for the fragment
    return inflater.inflate(R.layout.fragment_map_google, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}

fragment_map_google.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="probypro.bringmeinessen.MainActivity" />

On Manifest I add the permission and metadata

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

and my gradle app

dependencies {
compile 'com.google.firebase:firebase-core:10.2.1'
compile 'com.google.firebase:firebase-messaging:10.2.1'
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support:design:25.2.0'
compile 'com.google.android.gms:play-services-maps:10.2.1'
testCompile 'junit:junit:4.12'

compile 'com.google.zxing:core:3.2.1'

compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
compile 'com.android.volley:volley:1.0.0'
}

Is this has to do with the map loaded inside fragment? on the console error, the error point to code:

mapFragment.getMapAsync(this);

inside GoogleMapFragment.java

Here the error shown after the app shutted down:

05-19 15:03:13.072 27958-27958/probypro.bringmeinessen E/AndroidRuntime: FATAL EXCEPTION: main
                                                                     Process: probypro.bringmeinessen, PID: 27958
                                                                     java.lang.NullPointerException
                                                                         at probypro.bringmeinessen.GoogleMapFragment.onViewCreated(GoogleMapFragment.java:42)
                                                                         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1314)
                                                                         at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
                                                                         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
                                                                         at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:757)
                                                                         at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2355)
                                                                         at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146)
                                                                         at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2098)
                                                                         at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2008)
                                                                         at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
                                                                         at android.os.Handler.handleCallback(Handler.java:733)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                         at android.os.Looper.loop(Looper.java:149)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5257)
                                                                         at java.lang.reflect.Method.invokeNative(Native Method)
                                                                         at java.lang.reflect.Method.invoke(Method.java:515)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
                                                                         at dalvik.system.NativeStart.main(Native Method)

im not sure why it null.

I tried solution from @SilverSky to replace the line

SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);

but then i got another error log:

05-19 15:08:04.912 32566-1446/probypro.bringmeinessen 

E/Google Maps Android API: Authorization failure.  Please see https://developers.google.com/maps/documentation/android-api/start for how to correctly set up the map.
05-19 15:08:04.922 32566-1446/probypro.bringmeinessen E/Google Maps Android API: In the Google Developer Console (https://console.developers.google.com)
                                                                                 Ensure that the "Google Maps Android API v2" is enabled.
                                                                                 Ensure that the following Android Key exists:
                                                                                    API Key: <my api key>
                                                                                    Android Application (<cert_fingerprint>;<package_name>): 22:00:C3:0A:54:DF:D4:3A:8A:A2:58:6D:50:A1:E0:83:3C:13:4F:BD;probypro.bringmeinessen

This one is answered! Thank you dinesh for the help, the way to fix it is by replace getMap to getMapAsync.

try to follow this link:

Replace getMap with getMapAsync

Upvotes: 1

Views: 194

Answers (3)

Sandeep dhiman
Sandeep dhiman

Reputation: 1921

Change your getting map fragment line code with this

   SupportMapFragment mapFragment = (SupportMapFragment)getChildFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

Upvotes: 0

Dinesh Saini
Dinesh Saini

Reputation: 335

Change your fragment layout hope it will work fine

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="probypro.bringmeinessen.MainActivity">
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     />
    </FrameLayout>

Upvotes: 2

SilverSky
SilverSky

Reputation: 451

Just replace below line.

SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);

Upvotes: 0

Related Questions