简体   繁体   English

Google Maps在Android上崩溃-设置

[英]Google maps crash on android - setup

I want to add the map feature to my application. 我想将地图功能添加到我的应用程序。 I followed the steps on google and added all the good staff in my manifest. 我按照Google上的步骤进行操作,并在清单中添加了所有优秀人员。 here is the code: 这是代码:

<?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>

Now I Have an Activity with tabs ,each tab is a fragment, I want that one of the fragments will show a map (starched all over it) so I have created this map_frag.xml : 现在我有一个带有选项卡的Activity,每个选项卡都是一个片段,我希望其中一个片段将显示一张地图(上面都加满了星标),所以我创建了这个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>

and here is the fragment class: 这是片段类:

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;
     }
}

But there is a crash each time I opening the tab, here is the LogCat: 但是每次打开选项卡时都会发生崩溃,这是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

I don't understand why I get an error on Binary XML file . 我不明白为什么我在Binary XML file上出错。

There is one more thing: I cant import this GoogleMap class to my MapFragment as shown in this SO question. 还有一件事:我无法将此GoogleMap类导入到我的MapFragment ,如 SO问题所示。 Maybe there is something I need to add to my project 也许我需要添加一些内容

sorry dude you can not use fragment in MapFragment layout file because android does not support nested fragment through xml. 抱歉,您不能在MapFragment布局文件中使用片段,因为android不支持通过xml嵌套的片段。 you need to add fragment runtime. 您需要添加片段运行时。

1) Remove fragment from xml layout file. 1)从xml布局文件中删除片段。

2) Put there blank LinerarLayout like this 2)像这样放置空白的LinerarLayout

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

    </LinearLayout>

3) In your MapFragment.java 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);

Here is what I was missing in my manifest: 这是清单中我所缺少的:

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

Like @Vlad Ioffe said, try to add the below code into the manifest: 就像@Vlad Ioffe所说的那样,尝试将以下代码添加到清单中:

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

It works for me perfectly!!!! 它完全适合我!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM