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

4 回答 4

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_KEY 中填写了我的 api 密钥,而我应该只为值这样做。

于 2012-12-17T22:50:20.763 回答
1

您的 LogCat 显示另一个错误,例如:

12-17 17:16:20.366:E/AndroidRuntime(3634):原因:android.view.InflateException:二进制 XML 文件第 2 行:膨胀类片段时出错

它是所有问题的线索。如您所见,其中包含的“类”表达错误,但此时我不知道解决方案。

于 2013-04-10T12:12:44.203 回答
0

堆栈跟踪包括您的答案:

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

您需要获取 API 密钥并将其包含在清单中。看起来你有一些东西在那里,但也许它被编辑掉了?

于 2012-12-17T22:28:37.910 回答
0

尝试按照此获取新的 API 密钥https://docs.google.com/document/pub?id=19nQzvKP-CVLd7_VrpwnHfl-AE9fjbJySowONZZtNHzw

于 2012-12-18T09:33:08.280 回答