错误为地图增加类片段,附加我的代码

时间:2014-04-01 05:16:52

标签: android google-maps android-fragments

我的地图布局         

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.SupportMapFragment"/>

我的地图java

package com.example.tracker;
import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class Tracking extends FragmentActivity  {

    private GoogleMap map;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tracking);
 }
}

我的清单

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

    <uses-sdk 
        android:minSdkVersion="8" 
        android:targetSdkVersion="16" />
    <permission
        android:name="com.example.tracker.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />
    <uses-feature 
        android:glEsVersion="0x00020000"
        android:required="true" />

    <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" />
    <!-- The following two 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">

        <meta-data 
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity 
            android:name="com.example.tracker.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>

        <activity 
            android:name="com.example.tracker.Inbus"
            android:label="@string/app_name" />

        <activity 
            android:name="com.example.tracker.Inbay"
            android:label="@string/app_name" />

        <activity 
            android:name="com.example.tracker.Tracking"
            android:label="@string/app_name" />

        <meta-data 
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyBI3uLoMVsz-wVfTo3jAQboMfPt6n_zH5M" />
    </application>
</manifest>

最后是logcat错误

04-01 10:22:01.269: I/Adreno200-EGL(5511): Reconstruct Branch: AU_LINUX_ANDROID_JB_2.3.04.01.01.032.173 +  NOTHING
04-01 10:22:03.842: W/dalvikvm(5511): threadid=1: thread exiting with uncaught exception (group=0x40db9450)
04-01 10:22:03.862: E/AndroidRuntime(5511): FATAL EXCEPTION: main
04-01 10:22:03.862: E/AndroidRuntime(5511): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tracker/com.example.tracker.Tracking}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
04-01 10:22:03.862: E/AndroidRuntime(5511):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2062)
04-01 10:22:03.862: E/AndroidRuntime(5511):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2087)
04-01 10:22:03.862: E/AndroidRuntime(5511):     at android.app.ActivityThread.access$600(ActivityThread.java:133)
04-01 10:22:03.862: E/AndroidRuntime(5511):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1198)
04-01 10:22:03.862: E/AndroidRuntime(5511):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 10:22:03.862: E/AndroidRuntime(5511):     at android.os.Looper.loop(Looper.java:137)
04-01 10:22:03.862: E/AndroidRuntime(5511):     at android.app.ActivityThread.main(ActivityThread.java:4803)
04-01 10:22:03.862: E/AndroidRuntime(5511):     at java.lang.reflect.Method.invokeNative(Native Method)
04-01 10:22:03.862: E/AndroidRuntime(5511):     at java.lang.reflect.Method.invoke(Method.java:511)
04-01 10:22:03.862: E/AndroidRuntime(5511):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
04-01 10:22:03.862: E/AndroidRuntime(5511):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
04-01 10:22:03.862: E/AndroidRuntime(5511):     at dalvik.system.NativeStart.main(Native Method)
04-01 10:22:03.862: E/AndroidRuntime(5511): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
04-01 10:22:03.862: E/AndroidRuntime(5511):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
04-01 10:22:03.862: E/AndroidRuntime(5511):     at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
04-01 10:22:03.862: E/AndroidRuntime(5511):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-01 10:22:03.862: E/AndroidRuntime(5511):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
04-01 10:22:03.862: E/AndroidRuntime(5511):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:258)
04-01 10:22:03.862: E/AndroidRuntime(5511):     at android.app.Activity.setContentView(Activity.java:1867)
04-01 10:22:03.862: E/AndroidRuntime(5511):     at com.example.tracker.Tracking.onCreate(Tracking.java:11)
04-01 10:22:03.862: E/AndroidRuntime(5511):     at android.app.Activity.performCreate(Activity.java:5008)
04-01 10:22:03.862: E/AndroidRuntime(5511):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-01 10:22:03.862: E/AndroidRuntime(5511):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2026)
04-01 10:22:03.862: E/AndroidRuntime(5511):     ... 11 more
04-01 10:22:03.862: E/AndroidRuntime(5511): 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
04-01 10:22:03.862: E/AndroidRuntime(5511):     at android.app.Fragment.instantiate(Fragment.java:584)
04-01 10:22:03.862: E/AndroidRuntime(5511):     at android.app.Fragment.instantiate(Fragment.java:552)
04-01 10:22:03.862: E/AndroidRuntime(5511):     at android.app.Activity.onCreateView(Activity.java:4656)
04-01 10:22:03.862: E/AndroidRuntime(5511):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
04-01 10:22:03.862: E/AndroidRuntime(5511):     ... 20 more
04-01 10:22:03.862: E/AndroidRuntime(5511): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.MapFragment
04-01 10:22:03.862: E/AndroidRuntime(5511):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
04-01 10:22:03.862: E/AndroidRuntime(5511):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
04-01 10:22:03.862: E/AndroidRuntime(5511):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
04-01 10:22:03.862: E/AndroidRuntime(5511):     at android.app.Fragment.instantiate(Fragment.java:574)
04-01 10:22:03.862: E/AndroidRuntime(5511):     ... 23 more

2 个答案:

答案 0 :(得分:0)

好像你没有正确添加google-play-service库。 您必须从sdk路径导入google-play-service库项目。 并在项目中使用此库。

有关分步指南,请参阅此帖Google Map v2

答案 1 :(得分:0)

您需要从Android SDK管理器下载Google Play服务,如果您使用的是eclipse,则需要导入项目并添加为项目的依赖项是[sdk_folder] / extras / google / google_play_services / libproject / google-play-services_lib,你就完成了。

这是官方文档 请访问:http://developer.android.com/google/play-services/setup.html