Hi all I have build a here map api for mobile but something wrong in device a map not show. I'll follow this tutorial Creating a Simple Application Using the HERE SDK but in Logcat not show any error i don't know what i wrong please see my code
Here Map Class:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import com.here.android.mpa.common.GeoCoordinate;
import com.here.android.mpa.common.OnEngineInitListener;
import com.here.android.mpa.mapping.Map;
import com.here.android.mpa.mapping.MapFragment;
public class hereMap extends AppCompatActivity {
// map embedded in the map fragment
private Map map = null;
// map fragment embedded in this activity
private MapFragment mapFragment = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_here_map);
// Search for the map fragment to finish setup by calling init().
mapFragment = (MapFragment)getFragmentManager().findFragmentById(
R.id.mapfragment);
mapFragment.init(new OnEngineInitListener() {
#Override
public void onEngineInitializationCompleted(
OnEngineInitListener.Error error)
{
if (error == OnEngineInitListener.Error.NONE) {
// retrieve a reference of the map from the map fragment
map = mapFragment.getMap();
// Set the map center to the Vancouver region (no animation)
map.setCenter(new GeoCoordinate(49.196261, -123.004773, 0.0),
Map.Animation.NONE);
// Set the zoom level to the average between min and max
map.setZoomLevel(
(map.getMaxZoomLevel() + map.getMinZoomLevel()) / 2);
} else {
System.out.println("ERROR: Cannot initialize Map Fragment"+error.toString());
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_here_map, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here map Activity:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- Map Fragment embedded with the map object -->
<fragment
class="com.here.android.mpa.mapping.MapFragment"
android:id="#+id/mapfragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Manifest File:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mpat.bkklife" >
<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".LoginActivity"
android:label="#string/app_name"
android:windowSoftInputMode="adjustResize|stateVisible" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="com.here.android.maps.appid" android:value="My App ID:************"/>
<meta-data android:name="com.here.android.maps.apptoken" android:value="My App Token:***********"/>
<meta-data android:name="com.here.android.maps.license.key" android:value="My License Key:***********"/>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
</activity>
<activity
android:name=".hereMap"
android:label="#string/title_activity_here_map" >
</activity>
</application>
</manifest>
UPDATE
I got a error in log cat it say "ERROR: Cannot initialize Map Fragment MISSING_LIBRARY" when my app is init the map. But i already include HERE SDK and armeabi-v7a.
SpecDevice: Galaxsy S4 android 5.0.1 Lollipop
and this is my lib in project:
What i wrong of my code? Thank every one.
Try changing this line:
System.out.println("ERROR: Cannot initialize Map Fragment");
to this:
System.out.println("ERROR: Cannot initialize Map Fragment: " + error.toString());
then check the log to see what OnEngineInitListener.Error you are receiving. This will help you find the root cause of the issue.
As Shiv mentioned in the comments, based on the AndroidManifest you posted it looks like the culprit could be a missing appId appCode and/or license key.
As a side note, instead of System.out.println, you should really use android.util.Log
Also, did you fill in the following lines in the manifest file:
<meta-data android:name="com.here.android.maps.appid" android:value="My App ID"/>
<meta-data android:name="com.here.android.maps.apptoken" android:value="My App Token"/>
<meta-data android:name="com.here.android.maps.license.key" android:value="My License Key"/>
Please insert the appid, token and evaluation key that you obtained when registering the application.
Also, did you copy the native libraries from the SDK package ?
The structure should be:
"project root"\libs\armeabi-v7a*.so
It looks like the SDK package only includes native libraries for armeabi, so when you try to run on a device with a different CPU architecture (see https://developer.android.com/ndk/guides/abis.html), it can't find the proper binaries.
I'd be curious if you found a work-around, because this prevents my app from working on any device with a non armeabi CPU.
Please check which NAME SPACE you add with your application
Important: You must use the same package name as you have registered on developer.here.com. Failure to do so leads to a blank map to appear in your application.
some time we create application in short time so its might be possible to we miss few steps .
and if we are working on more then one apps its happens :)
Related
I want to display Google map in android application. I have done following steps.
Created a new android application project with the package name com.gaurav.googlemap
Downloaded Google Play Services SDK from SDK Manager
Imported 'google-play-services_lib' into current workspace
Linked 'google-play-services_lib' to my current project
(Project properties -> Android -> Add(Into Library Section) -> Selected 'google-play-services_lib'-> Apply -> Ok
Registered SHA-1 fingerprint with following command
keytool -list -v -keystore "%USERPROFILE%.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android
Generated Android API key from Google API console
Modified AndroidManifest.xml as below
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="17" />
<permission
android:name="com.gaurav.googlemap.permission.MAPS_RECEIVE"
android:protectionLevel="signature"></permission>
<uses-permission
android:name="com.gaurav.googlemap.permission.MAPS_RECEIVE"/>
<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="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<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" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyC3hd_PcjjfraFGfnx3UVi0FLO5AwgxFT8" />
<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>
</application>
Modified activity_main.xml as below
<RelativeLayout 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"
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="com.gaurav.googlemap.MainActivity" >
<fragment
android:id="#+id/map"
class="com.google.android.gms.maps.MapFragment"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
Modified MainActivity.java as below
package com.gaurav.googlemap;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And finally I ran project into Android emulator
But there is a exception in LogCat as below
02-14 16:41:46.947: E/AndroidRuntime(1260): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gaurav.googlemap/com.gaurav.googlemap.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
I have read many answers on similar questions, but I didn't get this problem fix.
I am having operating system Windows 8.1 (32-bit)
I have heard there is a different way to setup emulator to display Google map. Is this related to my problem?
Please help me to fix this problem. Thanks.
Add this in AndroidManifest.xml
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
i'm trying to build a app with a Google Maps Api V2, but i only got a blank screen with zoom buttons everytime. I think everything is fine in my code. I have all the permissions necessary, i correctly got the Key. I already delete debug.keystore to eclipse generate another one, but i still got the same blank screen with zoom buttons. Can anyone see if i doing something wrong in my codes ?
My Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.meu"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<permission
android:name="com.example.meu.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.meu.permission.MAPS_RECEIVE" />
<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" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="****My key here***" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<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>
</application>
My layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
My Java code:
package com.example.meu;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends Activity {
// Google Map
private GoogleMap googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
// Loading map
setContentView(R.layout.activity_main);
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* function to load map. If map is not created it will create it for you
* */
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),"Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
}
}
}
#Override
protected void onResume() {
super.onResume();
initilizeMap();
}
I already read a lot of things here, but no one could help me.
Firstly: As your map is a fragment (MAPS API V.2) you should extend your MainActivity as a FragmentActivity
public class MainActivity extends FragmentActivity {
instead of
public class MainActivity extends Activity {
Secondly: When casting the fragment as a map use the SupportMapFragment class (for better cmpatability between android versions) and getSupportFragmentManager() to find your map fragment.
((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
instead of
((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
Also: as per the comments of the question "asker"(!?): If you find that your app is working in an Emulator but not on an actual device, ensure that you use your Debug.Keystore when building the App.
Note: Ensure at all times that you are using backwards compatible support libraries across your entire app (If you are targeting multiple versions of android of course but I can't understand why anyone would limit their app to only a couple of versions?) and also that all your keys, credentials, Auths etc.. are properly set up in the Google developers console. https://console.developers.google.com/
After do what Ryno Coetzee suggest me, the app work on emulator but not on device. But the app doesnt work on device because when i export to a apk file, i was using another keystore instead debug.keystore. After that, my app finally work on device and on emulator.
Thanks !
I'm new to the Android world, and I need to create an app using Google Maps Android API. I follow the instructions in:
https://developers.google.com/maps/documentation/android/start
and whenever I start the AVD I get an error:
[2013-12-05 21:04:37 - Map] Failed to install Map.apk on device 'emulator-5554!
[2013-12-05 21:04:37 - Map] (null)
[2013-12-05 21:04:38 - Map] Launch canceled!
My MainActivity code:
package com.example.map;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
my MainActivity XML file:
<?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"
android:name="com.google.android.gms.maps.MapFragment"/>
and my Manifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.map"
android:versionCode="1"
android:versionName="1.0" >
<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 following two 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"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="19" />
<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.xxxxxxxxxHIDDENxxxxxxxx"
android:value="xxxxxxxxxHIDDENxxxxxxxx"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.map.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>
</application>
</manifest>
Also tried the code that it's provided here:
https://developers.google.com/maps/documentation/android/
being the MainActivity file code:
package com.example.map;
import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_activity);
// Get a handle to the Map Fragment
GoogleMap map = ((MapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
LatLng sydney = new LatLng(-33.867, 151.206);
map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
map.addMarker(new MarkerOptions()
.title("Sydney")
.snippet("The most populous city in Australia.")
.position(sydney));
}
}
Any Ideas?
The first thing I would recommend is to not show your API key to the public; this are supposed to be private to whomever is developing the application.
Also the format of this meta tag is wrong:
<meta-data
android:name="com.google.android.maps.v2.AIzaSyB7AMKbYsVajgb6D0zpq9wUODX9gdR2DhE"
android:value="AIzaSyB7AMKbYsVajgb6D0zpq9wUODX9gdR2DhE"/>
You are copying your API key at the end of the name. I strongly suggest you create a new API key and do not use this one that you have exposed to the public. There might be other errors, but without the stack trace is hard to say.
See this page: http://developer.android.com/google/play-services/setup.html
Specifically:
To test your app when using the Google Play services SDK, you must use
either:
A compatible Android device that runs Android 2.3 or higher and includes Google Play Store.
The Android emulator with an AVD that runs the Google APIs platform based on Android 4.2.2 or higher.
So make sure the emulator you're using is Android 4.2.2 or greater. If it is below this level, Google Play Services is not available on the emulator, and you must use a physical android device to test it out.
If you have to test on a lower-end device emulator, there are workarounds to this but they aren't officially supported by Google so your mileage may vary.
See this link:
Custom emulator that supports Google Maps API
I know it'a already many questions was like this, I read all of them, and can't find solution anyway. What i did:
I did all this steps fine from http://developer.android.com/google/play-services/setup.html#Install to install google play sdk
In my project property in Android tab I have "google play service library" ok, in Java Builder Path: "Android Private Libraries" i have google-play-service.jar, and this library are selected. I did Import lib project also.
Code for testing i took from http://developer.android.com/google/gcm/client.html.
My manifest file:
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.intent.RECEIVE" />
<permission
android:name="lt.vaziouju.vaziuoju.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="lt.vaziouju.vaziuoju.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="api key here" />
<receiver
android:name="lt.vaziouju.vaziuoju.GcmBroadcastReceiver"
android:permission="lt.vaziouju.vaziuoju.c2dm.permission.SEND" >
<intent-filter>
<action android:name="lt.vaziouju.vaziuoju.c2dm.intent.RECEIVE" />
<category android:name="lt.vaziouju.vaziuoju" />
</intent-filter>
</receiver>
<service android:name="lt.vaziouju.vaziuoju.GcmIntentService" />
<activity
...
</activity>
</application>
My java code:
package lt.vaziouju.vaziuoju;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
public class Test_GCM extends Activity {
private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
private static final String TAG = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test__gcm);
TextView mDisplay = (TextView) findViewById(R.id.display);
Context context = getApplicationContext();
// Check device for Play Services APK.
if (checkPlayServices()) {
// If this check succeeds, proceed with normal processing.
// Otherwise, prompt user to get valid Play Services APK.
}
}
// You need to do the Play Services APK check here too.
#Override
protected void onResume() {
super.onResume();
checkPlayServices();
}
/**
* Check the device to make sure it has the Google Play Services APK. If
* it doesn't, display a dialog that allows users to download the APK from
* the Google Play Store or enable it in the device's system settings.
*/
private boolean checkPlayServices() {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
GooglePlayServicesUtil.getErrorDialog(resultCode, this, PLAY_SERVICES_RESOLUTION_REQUEST).show();
} else {
Log.i(TAG, "This device is not supported.");
finish();
}
return false;
}
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.test__gcm, menu);
return true;
}
}
If you have any idea how I can it, it will be grate. I know I'm not good enough in android programming, and just started learning GCM but I'm already spend 6h to reading a google, and nothing.
Thanks for any idea.
Are you using Eclipse? If Eclipse sees a jar file for compilation, it does not necessarily include it into the apk. (You have to mention the jar in several places, and usually people do not remember in how many places.)
ritht-click on the project name -- build path -- configure build path -- order and export.
The same dialog has a Projects tab, this is where you specify projects you depend on.
If that does not work, put the jar into the libs subdirectory of your project.
I -think I- have done all the steps in these two links :
http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/
http://developer.android.com/google/play-services/setup.html
so please don't send them as answers.
I have downloaded the Maps API, copied it into Eclipse Workspace, marked it as library, referenced it in my project. But no success. I'm still getting the errors :
GoogleMap cannot be resolved to a type
MapFragment cannot be resolved to a type
Here's my MainActivity.Java:
package com.mapsapp.mapsappv1;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends Activity {
// Google Map
private GoogleMap googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* function to load map. If map is not created it will create it for you
* */
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
#Override
protected void onResume() {
super.onResume();
initilizeMap();
}
}
And here is my AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mapsapp.mapsappv1"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="com.mapsapp.mapsappv1.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.mapsapp.mapsappv1.permission.MAPS_RECEIVE" />
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="18" />
<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" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<!-- this line is part of my trials.. probably unnecessary : -->
<activity android:name="com.google.android.gms.UnusedStub" />
<activity
android:name="com.mapsapp.mapsappv1.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>
<!-- Google Maps API Key -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="myApiKeyGeneratedWithSHA1Key-ItShouldBeTrue" />
</application>
</manifest>
Maybe somebody had this error and knows how to solve it ?
and I have tried restarting eclipse, cleaning the project, creating the project from scratch (its actually v10 or something :) ) apparently I'm doing something wrong..
Any kind of help will be appreciated. But please don't paste the two links I have mentioned at the beginning.
Edit: Progress Report
#regeme's comment : import com.google.android.gms.maps.GoogleMap; helped getting rid of the error : GoogleMap cannot be resolved to a type
However, MapFragment error remains.
I have changed the code according #Imtiyaz Khalani's answer. The new code is this :
private void initilizeMap() {
if (mMap == null) {
//googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
// check if map is created successfully or not
if (mMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
But it gives the error : The method getSupportFragmentManager() is undefined for the type MainActivity
When this error happens, it is because the imports didn't work well.
Let's try this, select the Project with Right click and then select Properties.
now u go Android -> Library -> add .
select google-play-services_lib .
obs: u have to import the google_play_services_lib from your sdk directory to your workspace!
directory : \sdk\extras\google\google_play_services.
please tell me if it worked.
Sorry for this horrible grammar(english isn't my native language).
replace
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
to
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
Use FragmentActivity instead of Activity.
http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.GoogleMap;
Manually import the above statement your problem will be solved.
If you are getting the following errors, it's means that the GoogleMap class and the MapFragment class can't be found.
As those class are part of the google-play-service library, you have some problem with the way to reference it.
You can go over the first three steps of this guide I wrote:
Google Maps API V2
and make sure you are making it right.
Your original mapFragment should work just fine after importing the two packages mentioned earlier however you manifest is missing:
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />