谷歌地图Android API V2崩溃了。

6

我想在我的安卓手机上加载谷歌地图API。我尝试了所有我搜索到的方法,但它仍然崩溃。 这是我的MainActivity.java文件。

package com.example.androidmapsv2;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Show the Up button in the action bar.
        getActionBar().setDisplayHomeAsUpEnabled(true);
        GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

        GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

    }

}

这是我的布局activity_main.xml。
<?xml version="1.0" encoding="utf-8"?>
<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>

这是我的清单。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidmapsv2"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="16" />

    <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="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- Require OpenGL ES version 2 -->
    <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.androidmapsv2.MainActivity"
            android:label="aaa" >
            <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="AIzaSyDr9xN8L3sF6W2ZRWmTR7q0lhTeOWhXEfQ" />
    </application>

</manifest>

这是我获得的密钥 enter image description here 这是包含的库(在更新中解决) enter image description here 由于没有真实的安卓设备,我在模拟器上安装了它,在这个教程中。
这是日志。
   01-09 18:05:19.184: E/Trace(1366): error opening trace file: No such file or directory (2)
01-09 18:05:19.354: W/dalvikvm(1366): Unable to resolve superclass of Lcom/example/androidmapsv2/MainActivity; (5)
01-09 18:05:19.354: W/dalvikvm(1366): Link of class 'Lcom/example/androidmapsv2/MainActivity;' failed
01-09 18:05:19.365: D/AndroidRuntime(1366): Shutting down VM
01-09 18:05:19.365: W/dalvikvm(1366): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
01-09 18:05:19.384: E/AndroidRuntime(1366): FATAL EXCEPTION: main
01-09 18:05:19.384: E/AndroidRuntime(1366): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.androidmapsv2/com.example.androidmapsv2.MainActivity}: java.lang.ClassNotFoundException: com.example.androidmapsv2.MainActivity
01-09 18:05:19.384: E/AndroidRuntime(1366):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
01-09 18:05:19.384: E/AndroidRuntime(1366):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-09 18:05:19.384: E/AndroidRuntime(1366):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
01-09 18:05:19.384: E/AndroidRuntime(1366):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-09 18:05:19.384: E/AndroidRuntime(1366):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 18:05:19.384: E/AndroidRuntime(1366):     at android.os.Looper.loop(Looper.java:137)
01-09 18:05:19.384: E/AndroidRuntime(1366):     at android.app.ActivityThread.main(ActivityThread.java:4745)
01-09 18:05:19.384: E/AndroidRuntime(1366):     at java.lang.reflect.Method.invokeNative(Native Method)
01-09 18:05:19.384: E/AndroidRuntime(1366):     at java.lang.reflect.Method.invoke(Method.java:511)
01-09 18:05:19.384: E/AndroidRuntime(1366):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-09 18:05:19.384: E/AndroidRuntime(1366):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-09 18:05:19.384: E/AndroidRuntime(1366):     at dalvik.system.NativeStart.main(Native Method)
01-09 18:05:19.384: E/AndroidRuntime(1366): Caused by: java.lang.ClassNotFoundException: com.example.androidmapsv2.MainActivity
01-09 18:05:19.384: E/AndroidRuntime(1366):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
01-09 18:05:19.384: E/AndroidRuntime(1366):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
01-09 18:05:19.384: E/AndroidRuntime(1366):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
01-09 18:05:19.384: E/AndroidRuntime(1366):     at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
01-09 18:05:19.384: E/AndroidRuntime(1366):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
01-09 18:05:19.384: E/AndroidRuntime(1366):     ... 11 more

更新

我尝试将Google Play服务库复制到工作区,然后红色标记消失了 enter image description here 但是出现了另一个错误。它缺少google-play-services_lib.jar(lib文件夹名称+ .jar)。如何修复它? enter image description here 当我点击google-play-services_lib.jar时,删除按钮没有显示出来(就像图片中一样)。然而,我尝试删除所有Android依赖项并重新导入其他库。现在它可以工作,但仍然会崩溃。

我已经尝试更改

MainActivity extends Activity

To

MainActivity extends FragmentActivity

布局

<?xml version="1.0" encoding="utf-8"?>
<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>

出现了另一个错误,求助,我已经没有办法解决了。

最终,我解决了我的错误。我猜是因为我的Eclipse。当你添加库类时,它会自动出现“google-play-services_lib.jar”,你不需要做任何事情。我重新安装了Eclipse,一切正常运行。 enter image description here


只需要选择 google-play-services_lib.jar,然后移除库即可。 - Rahul Patel
谢谢你帮我,但我认为这是关于库附加和Eclipse的某种错误。我尝试了一个在线示例,它也有相同的错误。 - Fido
@Rahul Patel 我是Stack Overflow的新手。怎么做呢? 不管怎样,我通过重新安装我的Eclipse和SDK解决了我的问题。这是导入libs google-play-services_lib.jar的问题。 - Fido
@Fido 点击在 赞成-反对 按钮下方的右侧按钮。 - Rahul Patel
@Rahul Patel 谢谢,我已经接受、点赞并更新了我的解决方案,以便其他人参考。 - Fido
5个回答

2

我也曾经遇到过这种问题,当第一次使用API V2编写地图时。

尝试使用FragmentActivity扩展而不是Activity

你需要将项目(goolge_play_services)复制到工作区中,然后将其附加到你的项目上。在上面的图片中显示了一个红色的交叉,所以选择它并删除,然后再次添加并将库与项目附加。


1
我从Activity改为了FragmentActivity,但它仍然崩溃并出现另一个错误。 - Fido
请在您的SDK管理器中安装Google Play服务。如果未显示,请更新您的ADT插件。 - Rahul Patel

1

如果API版本低于11,则需要使用SupportMapFragment

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"/>
  • 重要提示

    不要忘记使用FragmentActivity扩展您的Activity


0
你需要编辑应用程序的AndroidManifest.xml文件,并在元素中添加以下声明。这将嵌入应用程序编译时使用的Google Play服务版本。
<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

0

你需要将项目(google_play_services)复制到你的工作区,然后将其附加到你的项目中。

你所做的是导入项目,但没有勾选复制项目到工作区,因此它会在添加库时显示错误标记。

因此,在导入google_play_services项目时,请勾选复制项目到工作区,然后将其附加到你的项目中。


谢谢,我已将其导入我的工作区,但出现了另一个错误。我已在第一篇帖子中更新了错误信息,请问如何修复它? - Fido
请检查您是否将兼容性jar文件添加到项目中,并使用FragmentActivity扩展您的活动。 - TNR
我修改了代码,但出现了另一个错误,我已经在第一篇帖子中更新了它。你能帮我吗? - Fido

0

从这里我可以看到你正在使用api级别8,而据我所知,“Fragments”是从11开始支持的。

所以在这里,您需要使用兼容的片段库。

同时,请确保您还在工作区中导入了Google Play服务库并将其添加到应用程序中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接