FATAL EXCEPTION:main java.lang.RuntimeException:无法启动活动ComponentInfo

时间:2013-05-08 10:48:58

标签: android google-maps map

我正在使用android API v2处理Map视图,我正在关注this tutorial,我已经按照他在教程中解释的那样,但我仍然遇到致命异常。这是我的日志猫的xml代码。

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

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

</RelativeLayout>

logcat的:

05-08 10:38:22.121: E/AndroidRuntime(954): FATAL EXCEPTION: main
05-08 10:38:22.121: E/AndroidRuntime(954): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.map/com.example.map.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.os.Looper.loop(Looper.java:137)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.app.ActivityThread.main(ActivityThread.java:5041)
05-08 10:38:22.121: E/AndroidRuntime(954):  at java.lang.reflect.Method.invokeNative(Native Method)
05-08 10:38:22.121: E/AndroidRuntime(954):  at java.lang.reflect.Method.invoke(Method.java:511)
05-08 10:38:22.121: E/AndroidRuntime(954):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-08 10:38:22.121: E/AndroidRuntime(954):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-08 10:38:22.121: E/AndroidRuntime(954):  at dalvik.system.NativeStart.main(Native Method)
05-08 10:38:22.121: E/AndroidRuntime(954): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-08 10:38:22.121: E/AndroidRuntime(954):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.app.Activity.setContentView(Activity.java:1881)
05-08 10:38:22.121: E/AndroidRuntime(954):  at com.example.map.MainActivity.onCreate(MainActivity.java:12)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.app.Activity.performCreate(Activity.java:5104)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-08 10:38:22.121: E/AndroidRuntime(954):  ... 11 more
05-08 10:38:22.121: E/AndroidRuntime(954): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has an empty constructor that is public
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.app.Fragment.instantiate(Fragment.java:592)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.app.Fragment.instantiate(Fragment.java:560)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.app.Activity.onCreateView(Activity.java:4709)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
05-08 10:38:22.121: E/AndroidRuntime(954):  ... 21 more
05-08 10:38:22.121: E/AndroidRuntime(954): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.MapFragment" on path: /data/app/com.example.map-2.apk
05-08 10:38:22.121: E/AndroidRuntime(954):  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
05-08 10:38:22.121: E/AndroidRuntime(954):  at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
05-08 10:38:22.121: E/AndroidRuntime(954):  at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
05-08 10:38:22.121: E/AndroidRuntime(954):  at android.app.Fragment.instantiate(Fragment.java:582)
05-08 10:38:22.121: E/AndroidRuntime(954):  ... 24 more

我的清单看起来像这样。

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

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

     <permission
        android:name="com.example.map.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />
    <uses-permission android:name="com.example.map.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <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="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="com.example.map.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="AIzaSyDSW0CfwDN0LyrF8PBeihEjrAkj25TDhUQ" />
    </application>

</manifest>

6 个答案:

答案 0 :(得分:1)

确保您在项目构建目标中使用Google API。转到项目属性&gt; Android检查这个。

答案 1 :(得分:1)

您正尝试将现有项目导入库中。您应该使用以下方法导入库。

  1. 右键单击包资源管理器。导入 - &gt;展开Android - &gt;现有 Android Code Into Workspace。
  2. 选择google-play-services库。并检查副本 工作区复选框。
  3. 一旦完成。参考图书馆项目

    右键单击该项目。在左侧窗格中选择Android。在右侧窗格底部,您将看到一个带有标签库的框架。单击添加并添加导入的库。

    要向项目添加支持库: 右键单击项目Androids Tools - &gt;添加支持库。

    如果您决定使用支持库,则必须替换代码

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

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

    同样在你的java文件中你必须使用SupportMapFragment。

    mMap = ((SupportMapFragment) getSupportFragmentManager()
                        .findFragmentById(R.id.map)).getMap();
    

答案 2 :(得分:0)

您是否在Eclipse中使用了正确的API(GOOGLE API)?如果您想在Android中使用谷歌地图,请务必使用GOOGLE API。

答案 3 :(得分:0)

您可能遇到导入问题。只需右键单击已导入库的project -> properties -> Java build path。有关清晰度,请参阅this link

答案 4 :(得分:0)

此错误通常源自google-play-services库的错误引用。

要正确地执行此操作,请按照我撰写的关于在您的应用中集成Google Maps API V2的博客文章指南的前3个步骤进行操作:

Google Maps API V2

答案 5 :(得分:0)

""Caused by: java.lang.ClassNotFoundException: Didn't find class"com.google.android.gms.maps.MapFragment" on path: /data/app/com.example.map-2.apk""

你检查过这个吗?

我认为您应该在AndroidManifest.XML中添加此(com.google.android.gms.maps.MapFragment)ACTIVITY。

尝试使用LogCat中的三个“由......引起的”错误。