gpt4 book ai didi

java - Android 上未显示 Google map 。只有谷歌标签

转载 作者:行者123 更新时间:2023-11-29 05:11:15 25 4
gpt4 key购买 nike

所以我在 Android Studio 上制作了一个 Android 应用程序,它向我展示了 Google map (这不是全部,但这是唯一相关的事情)。问题是,当我将 map 加载到 fragment 上时,唯一显示的是左下角的 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>

结果

Screenshot of the GoogleMaps

我遵循了所有可用的教程,但我担心我可能犯了错误,即使我不确定在哪里。

最佳答案

首先你应该改变

public class MainActivity extends FragmentActivity

public class MainActivity extends Activity

因为你的 minsdk>12 还有你正在使用 MapFragment

还有一件事

你应该交叉检查你的 map API key

关于java - Android 上未显示 Google map 。只有谷歌标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28364422/

25 4 0