77602

Android Google Maps v2 Stopping

Question:

I am new to Android and Google Maps v2 API. I tried to integrate the API in my app but I am getting:

<blockquote>

Unfortunately, Map Example has stopped.

</blockquote>

Can anyone help me finding the mistake?

MainActivity class:

import android.app.Activity; import android.os.Bundle; import android.widget.Toast; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.MapFragment; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); GoogleMap map; map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)) .getMap(); if (map == null) Toast.makeText(getApplicationContext(), "Sorry! unable to create Map.", Toast.LENGTH_SHORT).show(); else { // do somework here. } } }

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" > <!-- Google Maps API V2 --> <fragment android:id="@+id/map" android:name="com.google.android.gms.maps.MapFragment" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>

Androidmainfest.xml

<permission android:name="com.example.example.permission.MAPS_RECEIVE" android:protectionLevel="signature" > </permission> <uses-permission android:name="com.example.mapexample.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" /> <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="21" /> <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" > </activity> <activity android:name=".Sample" android:label="@string/title_activity_sample" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- <uses-library android:name="com.google.android.maps"/> --> <meta-data android:name="com.google.android.gms.version" android:value="6587000" /> <!-- API KEY --> <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="API-KEY-HERE" /> </application>

My logcat output

06-10 21:03:06.610: E/dalvikvm(857): Could not find class 'com.google.android.gms.maps.MapFragment', referenced from method com.example.mapexample.MainActivity.onCreate 06-10 21:03:06.610: W/dalvikvm(857): VFY: unable to resolve check-cast 1271 (Lcom/google/android/gms/maps/MapFragment;) in Lcom/example/mapexample/MainActivity; 06-10 21:03:06.610: D/dalvikvm(857): VFY: replacing opcode 0x1f at 0x0014 06-10 21:03:06.670: D/AndroidRuntime(857): Shutting down VM 06-10 21:03:06.670: W/dalvikvm(857): threadid=1: thread exiting with uncaught exception (group=0xb1ae6d70) 06-10 21:03:06.880: E/AndroidRuntime(857): FATAL EXCEPTION: main 06-10 21:03:06.880: E/AndroidRuntime(857): Process: com.example.mapexample, PID: 857 06-10 21:03:06.880: E/AndroidRuntime(857): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mapexample/com.example.mapexample.MainActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class fragment 06-10 21:03:06.880: E/AndroidRuntime(857): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2197) 06-10 21:03:06.880: E/AndroidRuntime(857): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2258) 06-10 21:03:06.880: E/AndroidRuntime(857): at android.app.ActivityThread.access$800(ActivityThread.java:138) 06-10 21:03:06.880: E/AndroidRuntime(857): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209) 06-10 21:03:06.880: E/AndroidRuntime(857): at android.os.Handler.dispatchMessage(Handler.java:102) 06-10 21:03:06.880: E/AndroidRuntime(857): at android.os.Looper.loop(Looper.java:136) 06-10 21:03:06.880: E/AndroidRuntime(857): at android.app.ActivityThread.main(ActivityThread.java:5026) 06-10 21:03:06.880: E/AndroidRuntime(857): at java.lang.reflect.Method.invokeNative(Native Method) 06-10 21:03:06.880: E/AndroidRuntime(857): at java.lang.reflect.Method.invoke(Method.java:515) 06-10 21:03:06.880: E/AndroidRuntime(857): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777) 06-10 21:03:06.880: E/AndroidRuntime(857): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 06-10 21:03:06.880: E/AndroidRuntime(857): at dalvik.system.NativeStart.main(Native Method) 06-10 21:03:06.880: E/AndroidRuntime(857): Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class fragment 06-10 21:03:06.880: E/AndroidRuntime(857): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713) 06-10 21:03:06.880: E/AndroidRuntime(857): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 06-10 21:03:06.880: E/AndroidRuntime(857): at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 06-10 21:03:06.880: E/AndroidRuntime(857): at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 06-10 21:03:06.880: E/AndroidRuntime(857): at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 06-10 21:03:06.880: E/AndroidRuntime(857): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:309) 06-10 21:03:06.880: E/AndroidRuntime(857): at android.app.Activity.setContentView(Activity.java:1930) 06-10 21:03:06.880: E/AndroidRuntime(857): at com.example.mapexample.MainActivity.onCreate(MainActivity.java:15) 06-10 21:03:06.880: E/AndroidRuntime(857): at android.app.Activity.performCreate(Activity.java:5242) 06-10 21:03:06.880: E/AndroidRuntime(857): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 06-10 21:03:06.880: E/AndroidRuntime(857): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161) 06-10 21:03:06.880: E/AndroidRuntime(857): ... 11 more 06-10 21:03:06.880: E/AndroidRuntime(857): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has an empty constructor that is public 06-10 21:03:06.880: E/AndroidRuntime(857): at android.app.Fragment.instantiate(Fragment.java:597) 06-10 21:03:06.880: E/AndroidRuntime(857): at android.app.Fragment.instantiate(Fragment.java:561) 06-10 21:03:06.880: E/AndroidRuntime(857): at android.app.Activity.onCreateView(Activity.java:4788) 06-10 21:03:06.880: E/AndroidRuntime(857): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689) 06-10 21:03:06.880: E/AndroidRuntime(857): ... 21 more 06-10 21:03:06.880: E/AndroidRuntime(857): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.MapFragment" on path: DexPathList[[zip file "/data/app/com.example.mapexample-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.mapexample-1, /system/lib]] 06-10 21:03:06.880: E/AndroidRuntime(857): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) 06-10 21:03:06.880: E/AndroidRuntime(857): at java.lang.ClassLoader.loadClass(ClassLoader.java:497) 06-10 21:03:06.880: E/AndroidRuntime(857): at java.lang.ClassLoader.loadClass(ClassLoader.java:457) 06-10 21:03:06.880: E/AndroidRuntime(857): at android.app.Fragment.instantiate(Fragment.java:583) 06-10 21:03:06.880: E/AndroidRuntime(857): ... 24 more

Answer1:

ClassNotFoundException, so mostly you have missed adding the library dependency. The Google Play Services need to be setup before using them into your project. Find details in the following link:

<a href="https://developers.google.com/android/guides/setup" rel="nofollow">https://developers.google.com/android/guides/setup</a>

As per official documentation here <a href="https://developers.google.com/maps/documentation/android/start" rel="nofollow">https://developers.google.com/maps/documentation/android/start</a>, the minimum required sdk version is 12. Please try increasing your minSdkVersion value to 12.

Answer2:

Your code seems fine. You may try this way, This is mentioned in the official docs,

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setUpMapIfNeeded() } private void setUpMapIfNeeded() { MapFragment mapFragment = (MapFragment) getFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(MainActivity.this); } @Override public void onMapReady(final GoogleMap googleMap) { // map has been loaded }

Make sure your class implements <strong>OnMapReadyCallback</strong>

Recommend

  • vuforia sdk + android failed to initialize Vuforia with permission exception
  • android.view.InflateException: Binary XML file line #12: Error inflating class fragment
  • GPS Location always remains active in notification tray after exit MapView
  • ActionBar Up Navigation not working on API 15
  • Android App should visible in playstore for normal screen devices as well as tablets and should appe
  • Android java.lang.SecurityException for system app
  • Android Map v2 not works in emulator errors are occurred in log cat
  • How to toggle “Use automatic configuration script”
  • Implementing long division using a generator function in python
  • doxygen generated documentation with auto-generated links to qt project
  • Shall the DB ConnectionString within Web.config be encrypted when using Azure Cloud?
  • Problem with Session State in Internet Explorer!
  • Is TWebBrowser dependant on IE version?
  • IdentityServer3 - redirect to ADFS if client is on intranet
  • Android Application: Got Internet Connection
  • Build a string in MSBuild as a concatenation of a base string n-times
  • Missing windows authentication IIS component because of Windows Vista Home Premium edition… any fixe
  • MapView: Could not find class A referenced from method B
  • How to write Delphi ActiveX control which can be used in MS Word/Excel/Powerpoint?
  • PowerShell execution policy subverted by powershell.exe parameter?
  • Android permissions - Manifest or android.manifest
  • Error:Connection timed out: connect. If you are behind an HTTP proxy, please configure the proxy set
  • How to replace one set of color 'shades' with another set
  • Launch Internet explorer from xna game on WP7
  • Sending emails over SMTP with TSL
  • How to access data/data files of Android without a terminal on the Android device
  • IllegalStateException “System services not available to Activities before onCreate()”
  • How to upload file on another domain?
  • SSLRead() return error -9806/15958)
  • How to remove left and right margins from all wrapped rows in flexbox (without nth-child or js)
  • init_seg and warning C4073 from library code?
  • Application bar icon text length
  • Is there a way to save the selected text and highlight it again once the page is refreshed?
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • nonblocking BIO_do_connect blocked when there is no internet connected
  • Marklogic : Query response time is very high
  • Abort upload large uploads after reading headers
  • java.lang.NoClassDefFoundError: com.parse.Parse$Configuration$Builder on below Lollipop versions
  • What is Eclipse's Declaration View used for?
  • how does django model after text[] in postgresql [duplicate]