我正在尝试运行Google地图的简单演示应用。我引用了标准文章。
但是我的错误导致类片段异常。
我已正确更新了mt清单。已注册Map API密钥。安装google-play-services并将其链接到我当前的项目。从FragmentActivity扩展我的活动,但它仍然无法正常工作。
我做错了吗?
我也提到Error inflating class fragment但无济于事。 任何帮助表示赞赏。
我的代码如下: activity_main.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"
tools:context=".MainActivity" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>
MainActivity.java
package com.android.googlemapsdemo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends FragmentActivity {
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(53.551, 9.993);
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
if (map != null) {
Marker hamburg = map.addMarker(new MarkerOptions()
.position(HAMBURG).title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
.position(KIEL)
.title("Kiel")
.snippet("Kiel is cool")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
} else {
map = ((MapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
if (map == null) {
Toast.makeText(MainActivity.this, "Unable to generate map",
Toast.LENGTH_SHORT).show();
}
}
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.googlemapsdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<permission
android:name="com.android.googlemapsdemo.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="com.android.googlemapsdemo.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<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" >
<activity
android:name="com.android.googlemapsdemo.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="AIzaSyAT9_V5YI-7CMf_Yta0Y_zeDfPZeH5XFL0" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
答案 0 :(得分:1)
我认为你应该改变这个
class="com.google.android.gms.maps.MapFragment"
到
class="com.google.android.gms.maps.SupportMapFragment"
因为您的minsdk="11"
也改变了这个
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
到
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
答案 1 :(得分:1)
更改此
<uses-sdk
android:minSdkVersion="11"
到
<uses-sdk
android:minSdkVersion="12"
如果你有11,你将使用SupportMapFragment
。考虑一下这个
https://developer.android.com/about/dashboards/index.html?utm_source=ausdroid.net
查看链接,最好更改为12
更改此
public class MainActivity extends FragmentActivity
到
public class MainActivity extends Activity
同样getMap()
可以返回null。更好地检查谷歌地图服务的可用性。
你也错过了
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
清单文件中的
您也可以删除
<permission
android:name="com.android.googlemapsdemo.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
和这个
<uses-permission android:name="com.android.googlemapsdemo.permission.MAPS_RECEIVE" />