Android application crashes when trying to load maps

710 views Asked by At

I am trying to launch map with in my application and it crashes with the below error:

11-23 21:23:16.345: E/AndroidRuntime(32288): FATAL EXCEPTION: main
11-23 21:23:16.345: E/AndroidRuntime(32288): java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable
11-23 21:23:16.345: E/AndroidRuntime(32288):    at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
11-23 21:23:16.345: E/AndroidRuntime(32288):    at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
11-23 21:23:16.345: E/AndroidRuntime(32288):    at android.app.Activity.onCreateView(Activity.java:4965)
11-23 21:23:16.345: E/AndroidRuntime(32288):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695)
11-23 21:23:16.345: E/AndroidRuntime(32288):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
11-23 21:23:16.345: E/AndroidRuntime(32288):    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
11-23 21:23:16.345: E/AndroidRuntime(32288):    at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
11-23 21:23:16.345: E/AndroidRuntime(32288):    at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
11-23 21:23:16.345: E/AndroidRuntime(32288):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:361)
11-23 21:23:16.345: E/AndroidRuntime(32288):    at android.app.Activity.setContentView(Activity.java:1956)
11-23 21:23:16.345: E/AndroidRuntime(32288):    at com.example.mymaps.MainActivity.onCreate(MainActivity.java:27)
11-23 21:23:16.345: E/AndroidRuntime(32288):    at android.app.Activity.performCreate(Activity.java:5372)
11-23 21:23:16.345: E/AndroidRuntime(32288):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
11-23 21:23:16.345: E/AndroidRuntime(32288):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
11-23 21:23:16.345: E/AndroidRuntime(32288):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
11-23 21:23:16.345: E/AndroidRuntime(32288):    at android.app.ActivityThread.access$700(ActivityThread.java:159)
11-23 21:23:16.345: E/AndroidRuntime(32288):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
11-23 21:23:16.345: E/AndroidRuntime(32288):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-23 21:23:16.345: E/AndroidRuntime(32288):    at android.os.Looper.loop(Looper.java:176)
11-23 21:23:16.345: E/AndroidRuntime(32288):    at android.app.ActivityThread.main(ActivityThread.java:5419)
11-23 21:23:16.345: E/AndroidRuntime(32288):    at java.lang.reflect.Method.invokeNative(Native Method)
11-23 21:23:16.345: E/AndroidRuntime(32288):    at java.lang.reflect.Method.invoke(Method.java:525)
11-23 21:23:16.345: E/AndroidRuntime(32288):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
11-23 21:23:16.345: E/AndroidRuntime(32288):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
11-23 21:23:16.345: E/AndroidRuntime(32288):    at dalvik.system.NativeStart.main(Native Method)

I searched for the error on the web and went through several other stackoverflow posts, but after following everything, i am not able to find any luck. Could anybody take a look at my code and point out what is the mistake that i am doing?

MainActivity.java:

package com.example.mymaps;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.graphics.Color;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.PolylineOptions;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {

    private static final LatLng LOWER_MANHATTAN = new LatLng(40.722543,
            -73.998585);
    private static final LatLng TIMES_SQUARE = new LatLng(40.7577, -73.9857);
    private static final LatLng BROOKLYN_BRIDGE = new LatLng(40.7057, -73.9964);

    private GoogleMap googleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setUpMapIfNeeded();
    }

    @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);
    }

    private void setUpMapIfNeeded() {
        // check if we have got the googleMap already
        if (googleMap == null) {
            googleMap = ((SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map)).getMap();
            if (googleMap != null) {
                addLines();
            }
        }
    }

    private void addLines() {

        googleMap
                .addPolyline((new PolylineOptions())
                        .add(TIMES_SQUARE, BROOKLYN_BRIDGE, LOWER_MANHATTAN,
                                TIMES_SQUARE).width(5).color(Color.BLUE)
                        .geodesic(true));
        // move camera to zoom on map
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LOWER_MANHATTAN,
                13));
    }


}

layout.xml:

<LinearLayout 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"
          android:name="com.google.android.gms.maps.MapFragment"/>

</LinearLayout>

Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mymaps"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="21" />

        <permission
        android:name="com.example.mymaps.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.mymaps.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="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=".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.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyCydrKy64z9rsUu5QP3bL_s03sMvCoIgFo" />

    </application>

</manifest>
1

There are 1 answers

0
An SO User On

Here is a question similar to yours: java.lang.noclassdeffounderror: com.google.android.gms.R$styleable

You will have to include the Google Play Services library in your projects. The instructions are given here: http://developer.android.com/google/play-services/setup.html

Here is a tutorial that will help you: http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/