i just learning about android programming to show map. but when i run this code ,there\'s some error. Can anybody explain why this error occurs? And what I can do to fix this pr
Above mentioned problem can occur if you have emulator or device in which google play services are not installed. I don't have perfect solution but I figured out some work around to save your app from crashing. In this case you have to follow simple steps 1. Override startActivityForResult(intent, requestcode) 2. In startActivityForResult add super call super.startActivityForResult in try catch and catch the NullPointerException
Easy its done Now you can trap the nullpointerexception here and add your error handling in catch
If you are testing this programm in device, your programm will works with Level 17 or higher, to use from 2.3.3 try to set your code as Siddharth Vyas Advice : Never show your map key to every body.
And make sure following steps done correct or not:
Steps:
android.library.reference.1=google-play-services_lib
-keep class * extends java.util.ListResourceBundle {
protected Object[][] getContents();
}
Okay, now you ready to create your own Google Map app with using Google Map APIs V2 for Android.
If you create application with min SDK = 8, please use android support library v4 + SupportMapFragment instead of MapFragment.
See here,just change the api key with your key in manifest file and follow these steps: and make sure that generate api key with package name which is mentioned in android manifest file and your google_play_services_lib project should be present in your project's work space only.
Manifest file:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<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" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.geeklabs.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>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="replace with your API key"/>
</application>
</manifest>
MainActivity.java:
package com.geeklabs.map;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
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"
android:name="com.google.android.gms.maps.MapFragment"/>
After got this let me know.
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();}
Hope this helps.