0

我想将地图功能添加到我的应用程序中。我按照谷歌上的步骤,在我的清单中添加了所有优秀的员工。这是代码:

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

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

    <uses-permission android:name="com.events.activities.permission.MAPS_RECEIVE" />

    <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-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.maps.v2.API_KEY"
            android:value="MY_API_KEY" />

        <activity
            android:name="com.events.activities.SplashActivity"
            android:label="@string/title_activity_splash" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.events.activities.LoginActivity"
            android:label="@string/title_activity_login" >
        </activity>
        <activity
            android:name="com.events.activities.MasterActivity"
            android:label="@string/title_activity_master" >
        </activity>
    </application>

</manifest>

现在我有一个带有选项卡的活动,每个选项卡都是一个片段,我希望其中一个片段显示一张地图(上面涂满了淀粉),所以我创建了这个map_frag.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" >

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

</RelativeLayout>

这是片段类:

public class MapFragment extends Fragment
{
    @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
     {
         View rootView = inflater.inflate(R.layout.map_frag, container, false);

         return rootView;
     }
}

但是每次我打开标签时都会发生崩溃,这里是 LogCat:

11-12 06:41:05.387: E/AndroidRuntime(1329): FATAL EXCEPTION: main
11-12 06:41:05.387: E/AndroidRuntime(1329): android.view.InflateException: Binary XML file line #6: Error inflating class fragment
11-12 06:41:05.387: E/AndroidRuntime(1329):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
11-12 06:41:05.387: E/AndroidRuntime(1329):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
11-12 06:41:05.387: E/AndroidRuntime(1329):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
11-12 06:41:05.387: E/AndroidRuntime(1329):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-12 06:41:05.387: E/AndroidRuntime(1329):     at com.events.fragments.MapFragment.onCreateView(MapFragment.java:23)
11-12 06:41:05.387: E/AndroidRuntime(1329):     at android.app.Fragment.performCreateView(Fragment.java:1695)
11-12 06:41:05.387: E/AndroidRuntime(1329):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
11-12 06:41:05.387: E/AndroidRuntime(1329):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
11-12 06:41:05.387: E/AndroidRuntime(1329):     at android.app.BackStackRecord.run(BackStackRecord.java:682)
11-12 06:41:05.387: E/AndroidRuntime(1329):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
11-12 06:41:05.387: E/AndroidRuntime(1329):     at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
11-12 06:41:05.387: E/AndroidRuntime(1329):     at android.os.Handler.handleCallback(Handler.java:725)
11-12 06:41:05.387: E/AndroidRuntime(1329):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-12 06:41:05.387: E/AndroidRuntime(1329):     at android.os.Looper.loop(Looper.java:137)
11-12 06:41:05.387: E/AndroidRuntime(1329):     at android.app.ActivityThread.main(ActivityThread.java:5041)
11-12 06:41:05.387: E/AndroidRuntime(1329):     at java.lang.reflect.Method.invokeNative(Native Method)
11-12 06:41:05.387: E/AndroidRuntime(1329):     at java.lang.reflect.Method.invoke(Method.java:511)
11-12 06:41:05.387: E/AndroidRuntime(1329):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-12 06:41:05.387: E/AndroidRuntime(1329):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-12 06:41:05.387: E/AndroidRuntime(1329):     at dalvik.system.NativeStart.main(Native Method)
11-12 06:41:05.387: E/AndroidRuntime(1329): Caused by: android.support.v4.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
11-12 06:41:05.387: E/AndroidRuntime(1329):     at android.support.v4.app.Fragment.instantiate(Fragment.java:409)
11-12 06:41:05.387: E/AndroidRuntime(1329):     at android.support.v4.app.Fragment.instantiate(Fragment.java:377)
11-12 06:41:05.387: E/AndroidRuntime(1329):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:277)
11-12 06:41:05.387: E/AndroidRuntime(1329):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
11-12 06:41:05.387: E/AndroidRuntime(1329):     ... 19 more
11-12 06:41:05.387: E/AndroidRuntime(1329): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.MapFragment" on path: /data/app/com.events.activities-2.apk
11-12 06:41:05.387: E/AndroidRuntime(1329):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
11-12 06:41:05.387: E/AndroidRuntime(1329):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
11-12 06:41:05.387: E/AndroidRuntime(1329):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
11-12 06:41:05.387: E/AndroidRuntime(1329):     at android.support.v4.app.Fragment.instantiate(Fragment.java:399)
11-12 06:41:05.387: E/AndroidRuntime(1329):     ... 22 more

我不明白为什么我在Binary XML file.

还有一件事: 我无法将此类导入GoogleMapMapFragment的,如this SO question所示。也许我需要在我的项目中添加一些东西

4

3 回答 3

0

就像@Vlad Ioffe 所说,尝试将以下代码添加到清单中:

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

它非常适合我!!!!

于 2013-11-12T09:03:54.117 回答
0

抱歉,您不能在 MapFragment 布局文件中使用片段,因为 android 不支持通过 xml 嵌套片段。您需要添加片段运行时。

1) 从 xml 布局文件中删除片段。

2)像这样放在那里空白的LinearLayout

<LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/lnrMap" >

    </LinearLayout>

3)在你的 MapFragment.java

SupportMapFragment mapFragment = new SupportMapFragment();
FragmentManager fm = getChildFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.lnrMap, mapFragment);
        ft.commit();

Timer t = new Timer();
    t.schedule(new TimerTask() {

        @Override
        public void run() {
            getActivity().runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    try {

                        googleMap = mapFragment.getExtendedMap();

                        if (googleMap != null) {
                        // Write your code for map operation here
                            t.cancel();
                        }


                    } catch (Exception e) {
                    }

                }
            });

        }
    }, 0, 500);
于 2013-11-12T07:24:20.197 回答
0

这是我在清单中缺少的内容:

<meta-data 
            android:name="com.google.android.gms.version" 
            android:value="@integer/google_play_services_version" />
于 2013-11-12T07:35:44.677 回答