我正在尝试在我的samsung s3 mini(4.1.2)上使用谷歌android映射V2但是我继续收到消息“应用程序已意外停止”。我加入了谷歌API [Android 4.1.2],谷歌播放服务库,android-support-v4.jar。 以下是代码: 在我的AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Test.projecttest"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="com.Test.projecttest.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.Test.projecttest.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!--
The following two permissions are not required to use
Google Maps Android API v2, but are recommended.
-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBjT4hZc4gKo1lyrQ5fwLD_Fz5vWgUQmlA" />
<activity
android:name="com.Test.projecttest.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>
</application>
</manifest>
com.Test.projecttest中的MainActivity.java
package com.Test.projecttest;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
main.xml位于res / layout
<?xml version="1.0" encoding="utf-8"?>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
我的logcat:
07-19 16:21:46.594: E/AndroidRuntime(5069): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Test.projecttest/com.Test.projecttest.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
07-19 16:21:46.594: E/AndroidRuntime(5069): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
07-19 16:21:46.594: E/AndroidRuntime(5069): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment: make sure class name exists, is public, and has an empty constructor that is public
07-19 16:21:46.594: E/AndroidRuntime(5069): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.SupportMapFragment
答案 0 :(得分:1)
你的minsdk是8
<uses-sdk
android:minSdkVersion="8"
使用SupportFragment
。
在布局xml中使用以下内容
<fragment
class="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
您的活动必须延长FragmentActivity
。
同时将此添加到清单
<permission
android:name="com.Test.projecttest.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.Test.projecttest.permission.MAPS_RECEIVE"/>
初始化地图对象
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
GoogleMap mMap = fm.getMap();
确保您已添加支持库
同时确保导入以下内容
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.SupportMapFragment;
答案 1 :(得分:0)
你需要在你的清单中添加:+所有@Raghunandan说;)
<permission
android:name="your.package.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="your.package.permission.MAPS_RECEIVE" />
答案 2 :(得分:0)
无需在xml中编写。
只需将活动扩展到SupportMapFragment
即可然后您就可以使用所有谷歌地图功能
并将其添加到清单:
<permission
android:name="com.Test.projecttest.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
我在片段上显示了地图,并将片段附加在MainActivity
上。它使它更灵活。我是通过以下方式做到的:
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnCameraChangeListener;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
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 MapFragment extends SupportMapFragment implements
LocationListener, OnMapClickListener, OnCameraChangeListener {