所以我正在Android Studio上制作一个Android应用程序,向我展示Google Maps(不是全部,但这是唯一相关的事情)。问题是,当我将地图加载到片段上时,唯一显示的是左下角的Google标记!

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>


结果



我遵循了所有可用的教程,即使我不确定在哪里也可能会犯一个错误。

最佳答案

首先你应该改变

public class MainActivity extends FragmentActivity




public class MainActivity extends Activity


因为您的minsdk>12以及您正在使用MapFragment

还有一件事

您应该交叉检查您的地图API key

10-04 16:12