简体   繁体   中英

Google Maps not showing on Android. Only the Google Tag

so i'm making an Android application on Android Studio that shows me the Google Maps (it's not all of it but it's the onlyue relevant thing t the case). The problem is that when i load the map onto the fragment, the only thing that shows up is the Google tag in the bottom left corner!

MainAtivity.java

public class MainActivity extends FragmentActivity{

    GoogleMap googleMap;
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        createMapView();

    }

    private void createMapView() {
        /**
         * Catch the null pointer exception that
         * may be thrown when initialising the map
         */
        try {
            if (null == googleMap) {
                googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                        R.id.map)).getMap();

                if(null != googleMap) {
                    Toast.makeText(getApplicationContext(),
                            "Mapa carregado!", Toast.LENGTH_SHORT).show();
                }

                /**
                 * If the map is still null after attempted initialisation,
                 * show an error to the user
                 */


                if (null == googleMap) {
                    Toast.makeText(getApplicationContext(),
                            "Erro ao criar mapa", Toast.LENGTH_SHORT).show();
                }
            }
        } catch (NullPointerException exception) {
            Log.e("mapApp", exception.toString());
        }

    }

}

activity_main.xml

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

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <fragment xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.google.android.gms.maps.MapFragment"
            android:id="@+id/map"
            android:layout_alignParentTop="true"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true" />
    </LinearLayout>
</RelativeLayout>

AndroidManifest.xml

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

 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

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

 <uses-permission android:name="com.example.gnr_p_v2.permission.MAPS_RECEIVE"/>
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 <uses-permission android:name="android.permission.READ_PHONE_STATE" />
 <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" />

 <uses-permission android:name="android.permission.CAMERA" />
 <uses-feature android:name="android.hardware.camera" />
 <uses-feature android:name="android.hardware.camera.autofocus" />

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

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Light.NoTitleBar">

        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
        <uses-library android:name="com.google.android.maps"/>

        <meta-data 
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyCIXfZQ3KbhU5nm61VzAbttdBAbT6jZ7xc"/>

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

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:windowSoftInputMode="stateHidden" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>
        </activity>


    </application>

</manifest>

Result

GoogleMaps的屏幕截图

I followed every tutorial available and i'm afraid I might have made a mistake even though I'm not sure where.

First you should change

public class MainActivity extends FragmentActivity

to

public class MainActivity extends Activity

because your minsdk>12 and also you are using MapFragment

And another thing

you should cross check your map API key

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