【问题标题】:Getting error "java.lang.RuntimeException: Unable to start activity" in my app在我的应用程序中出现错误“java.lang.RuntimeException:无法启动活动”
【发布时间】:2015-03-20 23:41:47
【问题描述】:

我的应用程序昨天工作了,我不知道它为什么停止工作。我唯一尝试做的就是创建一个签名的 apk。为此,我必须创建一个新的 Google 地图密钥并将其放入清单文件中。我是按照 Google 开发人员教程完成的。但是现在当我尝试运行它启动的应用程序时,我单击一个按钮并强制关闭。

这是错误日志:

  03-20 23:31:33.636  10036-10036/project.sharethefare E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: project.sharethefare, PID: 10036
        java.lang.RuntimeException: Unable to start activity ComponentInfo{project.sharethefare/project.sharethefare.CurrentLocation}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725)
                at android.app.ActivityThread.access$900(ActivityThread.java:172)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:145)
                at android.app.ActivityThread.main(ActivityThread.java:5834)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
         Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:770)
                at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
                at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:428)
                at android.app.Activity.setContentView(Activity.java:2241)
                at project.sharethefare.CurrentLocation.onCreate(CurrentLocation.java:24)
                at android.app.Activity.performCreate(Activity.java:6221)
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725)
                at android.app.ActivityThread.access$900(ActivityThread.java:172)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:145)
                at android.app.ActivityThread.main(ActivityThread.java:5834)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
         Caused by: java.lang.RuntimeException: The API key can only be specified once. It is recommended that you use the meta-data tag with the name: com.google.android.maps.v2.API_KEY in the <application> element of AndroidManifest.xml
                at com.google.maps.api.android.lib6.gmm6.c.g.a(Unknown Source)
                at com.google.maps.api.android.lib6.c.i.a(Unknown Source)
                at com.google.maps.api.android.lib6.c.el.a(Unknown Source)
                at com.google.maps.api.android.lib6.c.ab.a(Unknown Source)
                at com.google.maps.api.android.lib6.c.aa.a(Unknown Source)
                at com.google.android.gms.maps.internal.x.onTransact(SourceFile:107)
                at android.os.Binder.transact(Binder.java:380)
                at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
                at com.google.android.gms.maps.SupportMapFragment$a.onCreateView(Unknown Source)
                at com.google.android.gms.dynamic.a$4.b(Unknown Source)
                at com.google.android.gms.dynamic.a.a(Unknown Source)
                at com.google.android.gms.dynamic.a.onCreateView(Unknown Source)
                at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source)
                at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
                at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:920)
                at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
                at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1206)
                at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2159)
                at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)
                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
                at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
                at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:428)
                at android.app.Activity.setContentView(Activity.java:2241)
                at project.sharethefare.CurrentLocation.onCreate(CurrentLocation.java:24)
                at android.app.Activity.performCreate(Activity.java:6221)
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725)
                at android.app.ActivityThread.access$900(ActivityThread.java:172)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:145)
                at android.app.ActivityThread.main(ActivityThread.java:5834)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)

这是 CurrentLocation 类:

    package project.sharethefare;

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

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 android.location.Location;
import android.view.View;

public class CurrentLocation extends FragmentActivity {

    private GoogleMap mMap; // Might be null if Google Play services APK is not available.
    //________________________________________________________________________________________

    @Override
    protected void onCreate(Bundle savedInstanceState) {        //auto generated
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_current_location);
        setUpMapIfNeeded();                                     //part of google maps api
        mMap.setMyLocationEnabled(true);                               //creates a new HomeScreen
    }


    @Override
    protected void onResume() {                             //Auto Generated
        super.onResume();
        setUpMapIfNeeded();                                 //set up map if not already created
    }

    /**
     * Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
     * installed) and the map has not already been instantiated.. This will ensure that we only ever
     * call {@link #setUpMap()} once when {@link #mMap} is not null.
     * <p/>
     * If it isn't installed {@link SupportMapFragment} (and
     * {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
     * install/update the Google Play services APK on their device.
     * <p/>
     * A user can return to this FragmentActivity after following the prompt and correctly
     * installing/updating/enabling the Google Play services. Since the FragmentActivity may not
     * have been completely destroyed during this process (it is likely that it would only be
     * stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this
     * method in {@link #onResume()} to guarantee that it will be called.
     */
    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.curLocMap))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    /**
     * This is where we can add markers or lines, add listeners or move the camera. In this case, we
     * just add a marker near Africa.
     * <p/>
     * This should only be called once and when we are sure that {@link #mMap} is not null.
     */
    //call the method to continuously check current location
    private void setUpMap() {
        mMap.setOnMyLocationChangeListener(myLocationChangeListener);
    }

    //called above. Used to constantly update the users position on the map
    private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
        @Override
        public void onMyLocationChange(Location location) {
            //create a new latitude and longitude point
            LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
            //make global variables on home screen == to current location
            HomeScreen.curLat = location.getLatitude();
            HomeScreen.curLong = location.getLongitude();
            HomeScreen.curLocSet = true;
            //animate the camera to zoom in on position when found
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16.0f));
        }
    };

    public void backToHome(View view){ // called when button clicked. returns to homeScreen activity
        Intent intent = new Intent(CurrentLocation.this,HomeScreen.class);
        startActivity(intent);
    }

}

这是对应的xml文件:

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#000">

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"

        android:layout_marginTop="60dp"
        android:layout_marginBottom="50dp"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/curLocMap"
        tools:context="project.sharethefare.CurrentLocation"
        android:name="com.google.android.gms.maps.SupportMapFragment"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginBottom="70dp"
        android:text="@string/set_Cur_Loc"
        android:textSize="20sp"
        android:onClick="backToHome"
        android:background="@drawable/button_design"
        android:textStyle="bold"/>

</LinearLayout>

这是清单文件:

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

    <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-feature
        android:glEsVersion="0x00020000"
        android:required="true" />
    <!--
 The ACCESS_COARSE/FINE_LOCATION 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=".HomeScreen"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </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="" />

        <activity
            android:name=".CurrentLocation"
            android:label="@string/title_activity_current_location" >
        </activity>
        <activity
            android:name=".Destination"
            android:label="@string/title_activity_maps" >
        </activity>
        <activity
            android:name=".Share"
            android:label="@string/title_activity_share" >
        </activity>
        <activity
            android:name=".SplashScreen"
            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.geo.API_KEY"
            android:value=""/>

    </application>

</manifest>

【问题讨论】:

  • Caused by: java.lang.RuntimeException: API key 只能指定一次。建议您在 AndroidManifest.xml 的 元素中使用元数据标签,名称为:com.google.android.maps.v2.API_KEY

标签: java android google-maps


【解决方案1】:
Caused by: java.lang.RuntimeException: The API key can only be specified once. It is recommended that you use the meta-data tag with the name: com.google.android.maps.v2.API_KEY in the <application> element of AndroidManifest.xml

根据错误消息,将其从清单中删除:

<meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="AIzaSyCgOCmQbNnBUd_6HeB5DmTfHXPaxmmJJtk"/>

【讨论】:

  • 非常感谢你这个作品,有两个我从来没有注意到的键。但现在我的地图没有显示任何想法?
  • @McT:您可能想切换到getMapAsync() 而不是getMap()。他们还刚刚发布了一个新版本的 Play Services SDK,我还没有尝试过。您需要仔细检查您的 API 密钥值以确保它是正确的。除此之外,我唯一能建议的就是检查my directory of map samples,看看你是否可以让Basic 之类的工作正常工作,以确认这不是环境问题。
  • 好的,没问题,我的应用程序确实可以正常运行,但现在看起来有点乱,我一定会看看的。感谢您的帮助!
  • 如果您使用的是 Android Studio,并且您的项目有这个文件“google_maps_api.xml”,它应该在“.../Debug/Res/Values”下/”,您的 AndroidManifest.xml 文件中不能包含元数据,否则您将收到“API 密钥只能指定一次”错误
【解决方案2】:

如果您在应用程序中同时使用 Maps 和 Places Api,那么您只需要指定 geo api 键

<meta-data
    android:name="com.google.android.geo.API_KEY"
    android:value="AIzaSyCgOCmQbNnBUd_6HeB5DmTfHXPaxmmJJtk"/>

两个 api 都可以正常工作。您不能将两个元数据标签都用于 google api。

【讨论】:

  • 谢谢!我为此工作了几个小时,这就是问题所在。
【解决方案3】:

就我而言,我有 Google MapPlace API (PlaceLikelihoodBuffer)。

清单文件不支持 Google API 密钥的两个元数据

我已经评论了 Map 的 Google 地图 API 密钥。如下所示:

 <!-- <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyBqXxRIxL4nK7BarmN6qQB48kkAbdp1Kbk" />\  -->
      <meta-data
            android:name="com.crashlytics.ApiKey"
            android:value="5b03ad6e904c61e5d9bfe6e499e749671d9eaea4" />

幸运的是 Google MapPlace API (PlaceLikelihoodBuffer) 都对我有用。

奇怪但真实。

希望这会对你有所帮助。

【讨论】:

    猜你喜欢
    • 2016-01-07
    • 2015-08-18
    • 1970-01-01
    • 2017-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多