When I extend the Main.java by Activity it shows a black screen and mobile hang on but when i change it to FragmentActivity it works fine, what is the problem here?
Main.java
package com.uett.as;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import com.google.android.gms.maps.MapView;
//import com.google.android.maps.MapActivity;
//import com.google.android.maps.Overlay;
public class Main extends FragmentActivity {
// MapView map;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// map = (MapView) findViewById(R.id.map);
// map.setBuiltInZoomControls(true);
// Touchy t = new Touchy();
// List<Overlay> overlayList = map.getOverlays();
// overlayList.add(t);
}
#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
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
class Touchy extends Overlay{
public boolean onTouchEvent(MotionEvent e, MapView m){
return false;
}
}
*/
}
Main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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=".Main" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.uett.as"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<permission
android:name="com.uett.as.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.uett.as.permission.MAPS_RECEIVE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />
<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" >
<uses-library android:name="com.google.android.maps"/>
<activity
android:name="com.uett.as.Main"
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.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="Here is my Key"/>
</application>
</manifest>
Can anyone help me to see that what is the problem with this?????
You are using a fragment, but your minSdkVersion is 8. Which means you need to use the compatibility library's FragmentActivity to manage the fragment in your layout.
When I extend the Main.java by Activity it shows a black screen and
mobile hang on but when i change it to Fragment Activity it works fine
what is the problem here
You have
<uses-sdk
android:minSdkVersion="8"
Api level 11 and below you need to use SupportMapFragment. So you need to extend FragmentActivity. FragmentActivity is the base class for support based fragments
Remove
class="com.google.android.gms.maps.SupportMapFragment"
You already have
android:name="com.google.android.gms.maps.SupportMapFragment"
There is no need for
<permission
android:name="com.uett.as.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.uett.as.permission.MAPS_RECEIVE"/>
and this
<uses-library android:name="com.google.android.maps"/>
as of map api v2
Related
Basically I am trying to make an app which displays a Google Map.Here is my code.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<com.google.android.maps.MapView
android:id="#+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="my key goes here"
/>
</LinearLayout>
MainActivity class...
import com.google.android.maps.MapActivity;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends MapActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
When I run it,I get the following message
06-22 15:54:03.622: E/AndroidRuntime(26187): FATAL EXCEPTION: main
06-22 15:54:03.622: E/AndroidRuntime(26187): java.lang.RuntimeException:
Unable to instantiate activity
ComponentInfo{com.example.maptutorial/com.example.maptutorial.MainActivity}:
java.lang.ClassNotFoundException: com.example.maptutorial.MainActivity
Perhaps I might be doing something wrong in my Manifest File,and can not see it..
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.maptutorial"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.maptutorial.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>
you forgot to add uses-library in your manifest file,in this case
<application>
...
<uses-library android:name="com.google.android.maps"
android:required="true" />
...
</application>
I think you are using MAP API version 1 which is deprecated.
Why Cant you use API 2 either Fragment or support-fragment (https://developers.google.com/maps/documentation/android/start)
<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"/>
Manifest file
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyC8qEhdHVd3ZtxCR0549UqwnM72h0jgoMY" />
Basically I had to use the use the SupportMapFragment like this to make it work for android 2.3 .Also had to create a new API key..
<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"
tools:context=".MainActivity" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
And an sample of my java code is this.
public class EmergencyMap extends FragmentActivity {
static final LatLng ASTYNOMIA = new LatLng(39.620530, 22.401728);
static final LatLng PYROSVESTIKH = new LatLng(39.632652, 22.398704);
static final LatLng genikonosokomeio = new LatLng(39.642081, 22.422350);
static final LatLng panosokomeio = new LatLng(39.609993, 22.385722);
private GoogleMap map;
final int RQS_GooglePlayServices = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_emergency_map);
map = ((SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
map.addMarker(new MarkerOptions().position(ASTYNOMIA).title("ΑΣΤΥΝΟΜΙΑ"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(ASTYNOMIA,15));
map.animateCamera(CameraUpdateFactory.zoomTo(20),2000,null);
map.addMarker(new MarkerOptions().position(PYROSVESTIKH).title("ΠΥΡΟΣΒΕΣΤΙΚΗ"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(PYROSVESTIKH,15));
map.animateCamera(CameraUpdateFactory.zoomTo(20),2000,null);
map.addMarker(new MarkerOptions().position(genikonosokomeio).title("ΓΕΝΙΚΟ
ΝΟΣΟΚΟΜΕΙΟ"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(genikonosokomeio,15));
map.animateCamera(CameraUpdateFactory.zoomTo(20),2000,null);
map.addMarker(new MarkerOptions().position(panosokomeio).title("ΠΑΝΕΠΙΣΤΗΜΙΑΚΟ
ΝΟΣΟΚΟΜΕΙΟ"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(panosokomeio,15));
map.animateCamera(CameraUpdateFactory.zoomTo(20),2000,null);
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
}
protected void onResume() {
super.onResume();
int resultCode =
GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if (resultCode == ConnectionResult.SUCCESS){
Toast.makeText(getApplicationContext(),
"Το κινητό σας υποστηρίζει GooglePlayServices",
Toast.LENGTH_LONG).show();
}else{
GooglePlayServicesUtil.getErrorDialog(resultCode, this,RQS_GooglePlayServices);
}
}
}
It is Google Map API 2. Here is a part of my manifest file.
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<permission
android:name="com.example.larissaguide.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="larissa.app.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" />
<!--
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" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="larissa.app.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="MY_KEY_GOES_HERE" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
i have created an android app on google maps through eclipse,there are no errors but when i install it on emulator or any android device and run it,error message is displayed that "unfortunately app has stopped working".
please guide me for the troubleshooting.
this is the java main activity file :-
package com.example.droidloc;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import android.view.Menu;
import android.view.MotionEvent;
public class MainActivity extends MapActivity {
MapView map;
long start;
long stop;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map=(MapView)findViewById(R.id.mainmap);
map.setBuiltInZoomControls(true);
touchy t =new touchy();
List<Overlay> overlayList = map.getOverlays();
overlayList.add(t);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
class touchy extends Overlay
{
#SuppressWarnings("deprecation")
public boolean onTouchEvent(MotionEvent e,MapView m)
{
if(e.getAction()==MotionEvent.ACTION_DOWN)
{
start=e.getEventTime();
}
if(e.getAction()==MotionEvent.ACTION_UP)
{
stop=e.getEventTime();
}
if(stop-start>1500)
{
AlertDialog alert=new
AlertDialog.Builder(MainActivity.this).create();
alert.setTitle("Pick an Option");
alert.setMessage("Pick an Option dude");
alert.setButton2("Pin a Point", new
DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1)
{
// TODO Auto-generated method stub
}
});
alert.show();
return true;
}
return false ;
}
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
---------------------------------------------------------------------------------------
This is the xml file :-
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainmap"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"
android:enabled="true"
android:clickable="true"
/>
---------------------------------------------------------------------------------------
this is the manifest file :-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.droidloc"
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.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" >
<uses-library android:name="com.google.android.maps" />
<activity
android:name="com.example.droidloc.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="AIzaSyAlHKiaTa8KrABuGHIvcuZ4IiBFifqOu1s"/>
</application>
</manifest>
I think Your Fragment can not cast to MapView. so it could be your classcastexception . if not than you shoud post your logcat output.
try by use this in your Layout may be solve problem:
<com.google.android.gms.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mainmap"
android:layout_below="#+id/innerrelativelay" />
or
<fragment
android:id="#+id/mainmap"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
There are a bunch of things missing. Heres a list of what I saw, and some are from comments:
You are extending mapActivity which is based on versio 1 of google maps android. But in xml you are using mapfragment v2 of google maps android.
You should extend FragmentActivity or any other activity like ActionBarActivity that extends from FragmentActivity.
You are missing metadata tag in manifest file.
Min sdk version is 8 so you need to use SupportMapFragment in xml you are using mapfragment which is available above api 11.
I am trying to develop my first application for Android with Google Maps V2. I've copied one code from this website: source and I followed the instructions step by step. Now I am getting these errors in my Mainactivity and I don't know what is wrong.
I have an error in the following lines:
setContentView(R.layout.activity_main);
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
getMenuInflater().inflate(R.menu.main, menu);
Mainactivity.java
package info.tekguc.umut.googlemapsmapsandroidv2;
import info.tekguc.umut.googlemapsmapsandroidv2.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends Activity {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
final LatLng CIU = new LatLng(35.21843892856462, 33.41662287712097);
Marker ciu = mMap.addMarker(new MarkerOptions()
.position(CIU).title("My Office"));
}
#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;
}
}
activity_main.xml
<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=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"/>
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="info.tekguc.umut.googlemapsmapsandroidv2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<permission
android:name="com.example.androidmapsv2.permission.MAPS_RECEIVE"
android:protectionLevel="signature"></permission>
<uses-permission
android:name="com.example.androidmapsv2.permission.MAPS_RECEIVE"/>
<uses-permission
android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission
android:name="android.permission.INTERNET"/>
<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="AIzaSyDciL7-T3BphxGv2q-A77vNrcyJQ_sTrgI"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name="info.tekguc.umut.googlemapsmapsandroidv2.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>
Remove
import android.R;
and also you are missing a meta tag in manifest file
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
You should have
import info.tekguc.umut.googlemapsmapsandroidv2.R;
If it is same package no need to import
I tried everything, I researched and I can't get run my application. Each time I Run my application on my device get the message: "Sorry, Map has been has been interrupted". I done all the steps from this page https://developers.google.com/maps/documentation/android/start?hl=pl, and follow many tutorial and nothing. I hope you can save me.
Manifeste 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" >
<permission
android:name="com.example.map.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.map.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" />
<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>
<!-- Goolge Maps API Key -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBEGWDP9v4HepDF9A1NmSfFYjfdU2Jezm4" />
</application>
</manifest>
XML File:
<?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"/>
And my MainActivity:
package com.example.map;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
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();
}
}
#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;
}
/**
* 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();
}
}
Please Help me!!!!
try this :
class="com.google.android.gms.maps.SupportMapFragment"
on your activity :
SupportMapFragment mapfragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.showmap);
I think your problem in
android:name="com.google.android.gms.maps.MapFragment"
try instead of this, in the way:
class="com.google.android.gms.maps.MapFragment"
P.S. sometimes problem with maps is in not available Google Play Service on device.
(GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS)
must be true.
i am showing a map in activity ... but when i run app on device it does not show any thing , it show just white screen and zoom in zoom out options... MAP key is right .. thanks ..
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.edxample.finalmap"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<permission android:name="com.edxample.finalmap.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.edxample.finalmap.permission.MAPS_RECEIVE" />
<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" />
<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="com.edxample.finalmap.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="AIzaSyBL8ANi3jKkM0tF65C_Qus2_JgWRzClhfU" />
</application>
</manifest>
activity_main.xml
<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"
tools:context=".MainActivity" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:map="http://schemas.andoid.com/apk/es-auto"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
MainActivity
package com.edxample.finalmap;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
private GoogleMap googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(check())
{
setContentView(R.layout.activity_main);
Toast.makeText(this, "In Google play service.", Toast.LENGTH_LONG).show();
initFun();
}
else
{
Toast.makeText(this, "Google play service not available.", Toast.LENGTH_LONG).show();
}
}
private Boolean check()
{
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if(status==ConnectionResult.SUCCESS)
{
return true;
}
return false;
}
public void initFun()
{
SupportMapFragment sp = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
googleMap = sp.getMap();
googleMap.setMyLocationEnabled(true);
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Use your main packet name for generate key.
For example:
im my project, google map v2 is in com.ftravelbook.googlemapv2
but when generate key, you must use your main packet name (com.ftravelbook)
here are some screenshots:
I was also having the same problem because i have generated key by making on google maps api v2 on.It is used for web not for mobile.
For android device you have to make a google maps android api v2 on and generate your key
Is your map API key for your debugbuilds or your release builds? The key is based on your keystore, which is different between your debug and release build. Your release key for google maps won't work if you're debugging your app, and vice versa.