简体   繁体   中英

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. 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 :

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

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 .

There is one more thing: I cant import this GoogleMap class to my MapFragment as shown in this SO question. 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. you need to add fragment runtime.

1) Remove fragment from xml layout file.

2) Put there blank LinerarLayout like this

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

    </LinearLayout>

3) In your 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:

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

It works for me perfectly!!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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