0

I have written a very simple android app to display google map using google map api v2. But when I run the app, it always ends itself at once.

Anybody could help me to figure out the mistake? And I have import the google-play-services.jar, android-support-v4.jar and have add "Add support Library"

The main three file as follow:

1. MainActivity

package edu.lore;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.os.Bundle;

public class MainActivity extends android.support.v4.app.FragmentActivity {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setUpMapIfNeeded();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }
}

2.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/map"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  class="com.google.android.gms.maps.MapFragment"/>

3.AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.lore"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />    
<permission
    android:name="edu.lore.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />
<uses-permission android:name="edu.lore.permission.MAPS_RECEIVE" />

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyDFH436KisLnq4qY-55fVVOTePBIvMZOAo" />
</application>

sanjeeb
  • 87
  • 1
  • 12

3 Answers3

0

Could you please post the LogCat output?

I think a fragment is not allowed as root element of your layout file. The root element must be a ViewGroup, a View or a merge element. See http://developer.android.com/guide/topics/resources/layout-resource.html

userM1433372
  • 5,345
  • 35
  • 38
0

As the MapFragment is supported only for the api level 12 or above you need to use the SupportMapFragment instead as below:

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

and also check that you have enabled the service of Google Maps Android API v2 in google console where you have registered your application.

enter image description here

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
-1

It is required to use SupportMapFragment if MinSdkVersion is below 12

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"/>
moDev
  • 5,248
  • 4
  • 33
  • 63