Android Maps V2崩溃

时间:2012-12-17 22:10:39

标签: android google-maps google-maps-android-api-2

我今天的大项目是将我的应用迁移到新的Google Maps API V2。但是,我现在遇到一个错误,我似乎无法找到根本原因。

我的堆栈跟踪如下所示:

12-17 17:16:20.366: E/AndroidRuntime(3634): FATAL EXCEPTION: main
12-17 17:16:20.366: E/AndroidRuntime(3634): java.lang.RuntimeException: Unable to start        activity ComponentInfo{com.mobile.barwatch/com.mobile.barwatch.TestActivity}:     android.view.InflateException: Binary XML file line #2: Error inflating class fragment
12-17 17:16:20.366: E/AndroidRuntime(3634): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2088)
12-17 17:16:20.366: E/AndroidRuntime(3634): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2113)
12-17 17:16:20.366: E/AndroidRuntime(3634): at android.app.ActivityThread.access$700(ActivityThread.java:139)
12-17 17:16:20.366: E/AndroidRuntime(3634): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1224)
12-17 17:16:20.366: E/AndroidRuntime(3634): at android.os.Handler.dispatchMessage(Handler.java:99)
12-17 17:16:20.366: E/AndroidRuntime(3634): at android.os.Looper.loop(Looper.java:137)
12-17 17:16:20.366: E/AndroidRuntime(3634): at android.app.ActivityThread.main(ActivityThread.java:4918)
12-17 17:16:20.366: E/AndroidRuntime(3634): at java.lang.reflect.Method.invokeNative(Native Method)
12-17 17:16:20.366: E/AndroidRuntime(3634): at java.lang.reflect.Method.invoke(Method.java:511)
12-17 17:16:20.366: E/AndroidRuntime(3634): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
12-17 17:16:20.366: E/AndroidRuntime(3634): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
12-17 17:16:20.366: E/AndroidRuntime(3634): at dalvik.system.NativeStart.main(Native Method)
12-17 17:16:20.366: E/AndroidRuntime(3634): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
12-17 17:16:20.366: E/AndroidRuntime(3634): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
12-17 17:16:20.366: E/AndroidRuntime(3634): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
12-17 17:16:20.366: E/AndroidRuntime(3634): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
12-17 17:16:20.366: E/AndroidRuntime(3634): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
12-17 17:16:20.366: E/AndroidRuntime(3634): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:306)
12-17 17:16:20.366: E/AndroidRuntime(3634): at android.app.Activity.setContentView(Activity.java:1901)
12-17 17:16:20.366: E/AndroidRuntime(3634): at com.mobile.barwatch.TestActivity.onCreate(TestActivity.java:18)
12-17 17:16:20.366: E/AndroidRuntime(3634): at android.app.Activity.performCreate(Activity.java:5048)
12-17 17:16:20.366: E/AndroidRuntime(3634): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
12-17 17:16:20.366: E/AndroidRuntime(3634): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2052)
12-17 17:16:20.366: E/AndroidRuntime(3634):     ... 11 more
12-17 17:16:20.366: E/AndroidRuntime(3634): Caused by: java.lang.RuntimeException: API key not found.  Check that <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml
12-17 17:16:20.366: E/AndroidRuntime(3634):     at maps.y.z.a(Unknown Source)
12-17 17:16:20.366: E/AndroidRuntime(3634):     at maps.y.z.a(Unknown Source)
12-17 17:16:20.366: E/AndroidRuntime(3634):     at maps.y.z.a(Unknown Source)
12-17 17:16:20.366: E/AndroidRuntime(3634):     at maps.y.ae.a(Unknown Source)
12-17 17:16:20.366: E/AndroidRuntime(3634):     at maps.y.bu.a(Unknown Source)
12-17 17:16:20.366: E/AndroidRuntime(3634):     at maps.y.p.onCreateView(Unknown  Source)
12-17 17:16:20.366: E/AndroidRuntime(3634):     at com.google.android.gms.maps.internal.IMapFragmentDelegate$Stub.onTransact(IMapFragmentDelegate.java:107)
12-17 17:16:20.366: E/AndroidRuntime(3634):     at android.os.Binder.transact(Binder.java:326)
12-17 17:16:20.366: E/AndroidRuntime(3634):     at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
12-17 17:16:20.366: E/AndroidRuntime(3634):     at com.google.android.gms.maps.MapFragment$b.onCreateView(Unknown Source)
12-17 17:16:20.366: E/AndroidRuntime(3634):     at com.google.android.gms.internal.d$4.a(Unknown Source)
12-17 17:16:20.366: E/AndroidRuntime(3634):     at com.google.android.gms.internal.d.a(Unknown Source)
12-17 17:16:20.366: E/AndroidRuntime(3634):     at com.google.android.gms.internal.d.onCreateView(Unknown Source)
12-17 17:16:20.366: E/AndroidRuntime(3634):     at com.google.android.gms.maps.MapFragment.onCreateView(Unknown Source)
12-17 17:16:20.366: E/AndroidRuntime(3634):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:807)
12-17 17:16:20.366: E/AndroidRuntime(3634):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1013)
12-17 17:16:20.366: E/AndroidRuntime(3634):     at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1112)
12-17 17:16:20.366: E/AndroidRuntime(3634):     at android.app.Activity.onCreateView(Activity.java:4704)
12-17 17:16:20.366: E/AndroidRuntime(3634):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
12-17 17:16:20.366: E/AndroidRuntime(3634):     ... 20 more

我的活动课程如下:

package com.mobile.barwatch;

import android.app.Activity;
import android.os.Bundle;
import com.google.android.gcm.GCMRegistrar;
import com.google.android.gms.common.*;
import com.mobile.barwatch.tasks.*;
import com.mobile.barwatch.models.Constants;

public class TestActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {           
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_screen);              
    }
}

我的布局文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/mapview"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      class="com.google.android.gms.maps.MapFragment"/>

我的清单的相关部分看起来像这样:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mobile.barwatch"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="15" />
    <uses-feature
        android:glEsVersion="0x00020000"
    android:required="true"/>
    <permission android:name="com.mobile.barwatch.permission.MAPS_RECEIVE"
            android:protectionLevel="signature"/>
    <permission android:name="com.mobile.barwatch.permission.C2D_MESSAGE"
                android:protectionLevel="signature" />    
    <uses-permission android:name="com.mobile.barwatch.permission.MAPS_RECEIVE"/>
    <uses-permission android:name="com.mobile.barwatch.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <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="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <application    
        android:allowBackup="true"    
        android:icon="@drawable/ic_launcher_barwatch"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
    ....
    <activity android:name="TestActivity" 
              android:label="@string/app_name">
    </activity>
    <meta-data android:name="com.google.android.maps.v2.xx"
               android:value="xx"/>       
    </application>       
</manifest>

我真的把头靠在墙上,所以任何帮助都会非常感激。

4 个答案:

答案 0 :(得分:6)

好的,感谢A - C和qzikl的回复,我能够找出问题所在。在填写我的清单时,我有:

 <meta-data android:name="com.google.android.maps.v2.xx"
            android:value="xx"/>

其中xx是我的api密钥。实际上,它应该是:

 <meta-data android:name="com.google.android.maps.v2.API_KEY"
            android:value="xx"/>

因此导致我所有错误的原因是我使用我的api密钥填写API_KEY,我只应该为该值填写。

答案 1 :(得分:1)

你的LogCat显示了另一个错误:

  

12-17 17:16:20.366:E / AndroidRuntime(3634):引起:   android.view.InflateException:二进制XML文件行#2:错误   膨胀类片段

这是所有问题的线索。正如你所看到的那样,“类”中包含了错误的表达式,但此刻我还不知道解决方案。

答案 2 :(得分:0)

堆栈跟踪包括您的答案:

12-17 17:16:20.366:E / AndroidRuntime(3634):引起:java.lang.RuntimeException:找不到API密钥。检查它是否在AndroidManifest.xml的元素中

您需要获取API密钥并将其包含在清单中。看起来你已经有了一些东西,但也许它已被删除?

答案 3 :(得分:0)