I try to program an easy map app on Android but the only outcome are errors. I tried to program it the way it should be but it still doesn\'t work.
mainactivity<
you need to extend with FragmentActivity instead of Activity
Check my answer below :
In your xml file change this class="com.google.android.gms.maps.MapFragment" to android:name="com.google.android.gms.maps.SupportMapFragment"
Extend your activity to FragmentActivty and change
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)) .getMap(); to
map= ((SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map)).getMap();
Also check if (map== null) { map= ((SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map)).getMap();}
Check your manifest file :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.googlemapsv2"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="com.example.googlemapsv2.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.googlemapsv2.permission.MAPS_RECEIVE" />
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<!-- Requires OpenGL ES version 2 -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name="com.example.googlemapsv2.MainActivity"
android:label="@string/app_name"
android:theme="@style/AppBaseTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Google API Key -->
<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="your Google map API key" />
</application>
Hope this helps.
In your layout XML try to use
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.google.android.gms.maps.MapView>
instead this
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"/>
And import google play services on your project. Here I've explained how to do it