0

I am facing error when trying to navigate to the activity containing the Google maps. When navigating to the mapping activity it says, Unfortunately [application Name] has stopped Please advise.

AndroidManifest.xml `

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".DriverScoreActivity"
        android:label="@string/app_name"
        android:configChanges="locale|keyboardHidden|orientation|screenSize" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".CarHealthActivity"
        android:label="@string/app_name"
        android:configChanges="locale|keyboardHidden|orientation|screenSize">
    </activity>
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="@string/google_maps_key" />
</application>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!--
     The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
     Google Maps Android API v2, but are recommended.
-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

`

Layout Activity

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:background="#FFFFFF"
    android:gravity="center"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".DriverScoreActivity">
    <ImageView
        android:id="@+id/ivLogo"
        android:layout_width="146dp"
        android:layout_height="55dp"
        android:src="@drawable/logo" />

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:layout_below="@+id/ivLogo"
        android:layout_marginTop="4dp"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="samtechme.come.smartobd.VehicleLocationActivity" />
</RelativeLayout>

Display Map Code

 private void DisplayMap() {
    if (mMap == null) {
        mMap = ((MapFragment) getFragmentManager().findFragmentById(
                R.id.map)).getMap();
    }
    if (mMap != null) {
        // The Map is verified. It is now safe to manipulate the map.
        GoogleMapOptions options = new GoogleMapOptions();
        options.mapType(GoogleMap.MAP_TYPE_NORMAL);

        CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(
                25.023395, 55.090513));
        CameraUpdate zoom = CameraUpdateFactory.zoomTo(8);

        mMap.moveCamera(center);
        mMap.animateCamera(zoom);
    }
}

LogCat

             1521-1521/samtechme.come.smartobd E/AndroidRuntime﹕ FATAL EXCEPTION: main
        android.view.InflateException: Binary XML file line #18: Error inflating class fragment
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
        at android.app.Activity.setContentView(Activity.java:1895)
        at samtechme.come.smartobd.DriverScoreActivity.onOptionsItemSelected(DriverScoreActivity.java:213)
        at android.app.Activity.onMenuItemSelected(Activity.java:2566)
        at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:986)
        at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
        at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
        at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
        at com.android.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:156)
        at android.widget.AdapterView.performItemClick(AdapterView.java:298)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:2788)
        at android.widget.AbsListView$1.run(AbsListView.java:3463)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.ClassCastException: com.google.android.gms.maps.SupportMapFragment cannot be cast to android.app.Fragment
        at android.app.Fragment.instantiate(Fragment.java:585)
        at android.app.Fragment.instantiate(Fragment.java:560)
        at android.app.Activity.onCreateView(Activity.java:4738)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
Rizwan Javed
  • 3
  • 1
  • 3

2 Answers2

0

The reason is that your Map is not ready yet when you commit your Fragment and you are getting null when you use getMap() function.

The method to overcome this issue is described here in this question:

https://stackoverflow.com/a/19109093/1016462

Community
  • 1
  • 1
tasomaniac
  • 10,234
  • 6
  • 52
  • 84
  • You have to implement the callback and implement the method `onMapReady()`. In this method you should be able to get the map using `getMap()` function like you did in your code and it should not crash. – tasomaniac Oct 01 '14 at 11:25
0

you declared com.google.android.gms.maps.SupportMapFragment in xml, but you are using MapFragment cast in DisplayMap(). Change

mMap = ((MapFragment) getFragmentManager().findFragmentById(
            R.id.map)).getMap();

to

mMap = ((SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map)).getMap();
ashoke
  • 6,441
  • 2
  • 26
  • 25