0

我正在尝试在 android 应用程序上设置谷歌地图。我看到了很多关于这个错误的问题,但没有人真正回答我的问题

我的错误文件

03-15 22:32:56.235: D/AndroidRuntime(6713): Shutting down VM
03-15 22:32:56.235: W/dalvikvm(6713): threadid=1: thread exiting with uncaught exception (group=0x415b22a0)
03-15 22:32:56.235: E/AndroidRuntime(6713): FATAL EXCEPTION: main
03-15 22:32:56.235: E/AndroidRuntime(6713): java.lang.RuntimeException: Unable to start activity ComponentInfo{ant.project.myway/ant.project.myway.MainActivity}: android.view.InflateException:   Binary XML file line #7: Error inflating class fragment
03-15 22:32:56.235: E/AndroidRuntime(6713):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
03-15 22:32:56.235: E/AndroidRuntime(6713):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
03-15 22:32:56.235: E/AndroidRuntime(6713):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
03-15 22:32:56.235: E/AndroidRuntime(6713):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
03-15 22:32:56.235: E/AndroidRuntime(6713):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-15 22:32:56.235: E/AndroidRuntime(6713):     at android.os.Looper.loop(Looper.java:137)
03-15 22:32:56.235: E/AndroidRuntime(6713):     at android.app.ActivityThread.main(ActivityThread.java:4895)
03-15 22:32:56.235: E/AndroidRuntime(6713):     at java.lang.reflect.Method.invokeNative(Native Method)
03-15 22:32:56.235: E/AndroidRuntime(6713):     at java.lang.reflect.Method.invoke(Method.java:511)
03-15 22:32:56.235: E/AndroidRuntime(6713):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
03-15 22:32:56.235: E/AndroidRuntime(6713):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
03-15 22:32:56.235: E/AndroidRuntime(6713):     at dalvik.system.NativeStart.main(Native Method)
03-15 22:32:56.235: E/AndroidRuntime(6713): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
03-15 22:32:56.235: E/AndroidRuntime(6713):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
03-15 22:32:56.235: E/AndroidRuntime(6713):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
03-15 22:32:56.235: E/AndroidRuntime(6713):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
03-15 22:32:56.235: E/AndroidRuntime(6713):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-15 22:32:56.235: E/AndroidRuntime(6713):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
03-15 22:32:56.235: E/AndroidRuntime(6713):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:306)
03-15 22:32:56.235: E/AndroidRuntime(6713):     at android.app.Activity.setContentView(Activity.java:1912)
03-15 22:32:56.235: E/AndroidRuntime(6713):     at ant.project.myway.MainActivity.onCreate(MainActivity.java:11)
03-15 22:32:56.235: E/AndroidRuntime(6713):     at android.app.Activity.performCreate(Activity.java:5163)
03-15 22:32:56.235: E/AndroidRuntime(6713):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
03-15 22:32:56.235: E/AndroidRuntime(6713):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
03-15 22:32:56.235: E/AndroidRuntime(6713):     ... 11 more
03-15 22:32:56.235: E/AndroidRuntime(6713): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment: make sure class name exists, is public, and has an empty constructor that is public
03-15 22:32:56.235: E/AndroidRuntime(6713):     at android.support.v4.app.Fragment.instantiate(Fragment.java:401)
03-15 22:32:56.235: E/AndroidRuntime(6713):     at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
03-15 22:32:56.235: E/AndroidRuntime(6713):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
03-15 22:32:56.235: E/AndroidRuntime(6713):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
03-15 22:32:56.235: E/AndroidRuntime(6713):     ... 21 more
03-15 22:32:56.235: E/AndroidRuntime(6713): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.SupportMapFragment
03-15 22:32:56.235: E/AndroidRuntime(6713):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
03-15 22:32:56.235: E/AndroidRuntime(6713):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
03-15 22:32:56.235: E/AndroidRuntime(6713):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
03-15 22:32:56.235: E/AndroidRuntime(6713):     at android.support.v4.app.Fragment.instantiate(Fragment.java:391)
03-15 22:32:56.235: E/AndroidRuntime(6713):     ... 24 more
03-15 22:36:00.345: I/Process(6713): Sending signal. PID: 6713 SIG: 9

所以这是我的清单

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <permission
        android:name="ant.project.myway.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <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"/>
    <!-- The following two permissions are not required to use
     Google Maps Android API v2, but are recommended. -->
    <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="ant.project.myway.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="@string/api_key"/>
    </application>

</manifest>

我的活动

package ant.project.myway;

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

public class MainActivity extends FragmentActivity {

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

}

和布局

<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:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

</RelativeLayout>

看了很多帖子解决这个问题

我不确定我的 api 密钥是否正确,我在 ubuntu 上。我去~/.android,我跑

keytool -list -alias androiddebugkey -keystore debug.keystore -storepass android -keypass android

我得到了我的密钥,我在谷歌控制台 api 密钥上设置了 my_key;ant.project.myway

我认为这是正确的方法

谢谢你的帮助

4

3 回答 3

3

从 logcat 输出来看,您遇到的问题与您的密钥无关:

原因:android.support.v4.app.Fragment$InstantiationException:无法实例化片段 com.google.android.gms.maps.SupportMapFragment:确保类名存在,是公共的,并且有一个公共的空构造函数

基本上意味着在您的布局被实例化的那一刻,SupportMapFragment没有找到类。是什么让我认为您引用google-play-services库的方式有问题。

您可以查看我的博客文章,了解如何在您的应用程序中添加 Google 地图以查找更多信息。

于 2013-03-16T14:45:48.273 回答
1

如果您在“ant.project.myway”包 naem 的帮助下生成了密钥,那么您应该在权限标签中只使用这个包名称。

请试试这个...

<permission
        android:name="ant.project.myway.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

也使用 android:name 而不是类属性

  <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

还要在您的清单中添加此权限

<uses-permission android:name="ant.project.myway.permission.MAPS_RECEIVE" />
于 2013-03-16T09:28:48.113 回答
1

将此添加到您的活动中:

GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
于 2013-05-13T13:17:13.100 回答