得到错误" java.lang.RuntimeException:无法启动活动"在我的应用程序中

时间:2015-03-20 23:41:47

标签: java android google-maps

我的申请昨天工作,我无法弄清楚为什么它停止工作。我唯一想做的就是创建一个签名的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>

3 个答案:

答案 0 :(得分:6)

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"/>

答案 1 :(得分:4)

如果您同时使用地图和在您的应用程序中放置Api然后您只需要指定geo api密钥

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

两种apis都能正常工作。你不能同时使用谷歌apis的元数据标签。

答案 2 :(得分:1)

在我的情况下,我有谷歌地图&amp; 放置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" />

幸运的是谷歌地图&amp; 放置API PlaceLikelihoodBuffer )对我都有效。

奇怪但真实。

希望这会对你有所帮助。