简体   繁体   中英

Google Maps API V2 crash - FATAL EXCEPTION

I've tried a tutorial to implement a Google Maps in my application. http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/

I would try the example, but it is crashing at this level : setContentView(R.layout.activity_main);

I correctly follow the tuto, and normally there is no API Key problem.

EDIT : Here is all my solved code - Errors are shown in comment

MANIFEST.XML :

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

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

    <uses-permission android:name="com.example.essaimap.permission.MAPS_RECEIVE" />

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

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <!-- IMPORTANT ! DO NOT FORGET THIS META-DATA !!! -->
        <meta-data 
            android:name="com.google.android.gms.version" 
            android:value="@integer/google_play_services_version" />
        <!-- IMPORTANT ! DO NOT PUT THIS META IN ACTIVITY ELEMENT-->
        <meta-data 
                android:name="com.google.android.maps.v2.API_KEY"
                android:value="AIzaSyDWCZGT4OIUAZEOUIYAZEOIYAZOEIYAZOEIY6WRvbh6BnHSAipgg" />

        <activity
            android:name="com.example.essaimap.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <!-- I put my meta here, that is why my app was crashing 
            <meta-data 
                android:name="com.google.android.maps.v2.API_KEY"
                android:value="AIzaSyDWCZGT4n2HLZ5bUCM6WRvbh6BnHSAipgg" /> -->            
        </activity>
    </application>
</manifest>

ACTIVITY_MAIN.XML :

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

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

</RelativeLayout>

MAINACTIVITY.JAVA :

    public class MainActivity extends FragmentActivity // Not ACTIVITY
{
    // Google Map
    private GoogleMap googleMap;

    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try 
        {
            // Loading map
            initilizeMap();
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }

    /**
     * function to load map. If map is not created it will create it for you
     * */
    private void initilizeMap() 
    {
        if (googleMap == null) 
        {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

            // check if map is created successfully or not
            if (googleMap == null)
            {
                Toast.makeText(getApplicationContext(),"Sorry! unable to create maps",Toast.LENGTH_SHORT).show();
            }
        }
    }

    protected void onResume() 
    {
        super.onResume();
        initilizeMap();
    }
}

LOGCAT 我的应用程序的错误

My problem was solved thanks to @Piyush Gupta, @fasteque, @WarrenFaith, @Sankar V and @ViragBrahme.

Tofuw

You're missing a new mandatory meta-tag in your manifest file, it's written in the logcat.

Add the following line in your manifest file, in the application section:

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

Also, there's another issue in your manifest, you have copied the meta-tag com.google.android.maps.v2.API_KEY in your activity, while it shall be set in the application section as well.

FragmentActivity而不是Activity扩展您的类

Put meta-data out of activity and try

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

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

    <uses-permission android:name="com.example.essaimap.permission.MAPS_RECEIVE" />

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

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

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


    </application>
</manifest>

映射键元数据需要位于application标记内,而您试图将其放在activity标记内。

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