java - Android 应用程序在尝试加载 map 时崩溃

标签 java android android-maps-v2 android-maps

我尝试在我的应用程序中启动 map ,但它崩溃并出现以下错误:

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)

我在网上搜索了该错误,并浏览了其他几篇 stackoverflow 帖子,但在遵循所有内容后,我找不到任何运气。有人可以看一下我的代码并指出我犯的错误吗?

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


}

布局.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>

最佳答案

这是一个与您类似的问题:java.lang.noclassdeffounderror: com.google.android.gms.R$styleable

您必须在项目中包含 Google Play 服务库。此处给出了说明:http://developer.android.com/google/play-services/setup.html

这里是一个可以帮助您的教程:http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/

关于java - Android 应用程序在尝试加载 map 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27097427/

相关文章:

java - 如何通过parellel plesk更改tomcat中的默认JRE

java - Apache Camel 中的动态路由器 URI

javascript - 如何正确地为在 Android Studio 中创建的 map 设置动画以跟随标记

android - 检测 map 相机何时开始移动

android - ClusterManager 重绘 Google maps v2 utils 的标记

java - C与java之间的socket通信

java - 从 servlet 连接到数据库

android - 使用 Jackson 库进行 JSON 解析

安卓。 Firebase : Keystore file does not exist: ~/. android/debug.keystore

安卓摄像头录像