我正在关注使用google maps api v2的教程。我正面临一个错误 错误日志
05-15 00:08:18.415: E/AndroidRuntime(4547): FATAL EXCEPTION: main
05-15 00:08:18.415: E/AndroidRuntime(4547): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.rtesh.onemore/org.rtesh.onemore.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
05-15 00:08:18.415: E/AndroidRuntime(4547): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1830)
05-15 00:08:18.415: E/AndroidRuntime(4547): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851)
05-15 00:08:18.415: E/AndroidRuntime(4547): at android.app.ActivityThread.access$1500(ActivityThread.java:132)
05-15 00:08:18.415: E/AndroidRuntime(4547): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
05-15 00:08:18.415: E/AndroidRuntime(4547): at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 00:08:18.415: E/AndroidRuntime(4547): at android.os.Looper.loop(Looper.java:150)
05-15 00:08:18.415: E/AndroidRuntime(4547): at android.app.ActivityThread.main(ActivityThread.java:4277)
05-15 00:08:18.415: E/AndroidRuntime(4547): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 00:08:18.415: E/AndroidRuntime(4547): at java.lang.reflect.Method.invoke(Method.java:507)
05-15 00:08:18.415: E/AndroidRuntime(4547): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-15 00:08:18.415: E/AndroidRuntime(4547): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-15 00:08:18.415: E/AndroidRuntime(4547): at dalvik.system.NativeStart.main(Native Method)
05-15 00:08:18.415: E/AndroidRuntime(4547): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
05-15 00:08:18.415: E/AndroidRuntime(4547): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
05-15 00:08:18.415: E/AndroidRuntime(4547): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
05-15 00:08:18.415: E/AndroidRuntime(4547): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
05-15 00:08:18.415: E/AndroidRuntime(4547): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
05-15 00:08:18.415: E/AndroidRuntime(4547): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
05-15 00:08:18.415: E/AndroidRuntime(4547): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:250)
05-15 00:08:18.415: E/AndroidRuntime(4547): at android.app.Activity.setContentView(Activity.java:1712)
05-15 00:08:18.415: E/AndroidRuntime(4547): at org.rtesh.onemore.MainActivity.onCreate(MainActivity.java:27)
05-15 00:08:18.415: E/AndroidRuntime(4547): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
05-15 00:08:18.415: E/AndroidRuntime(4547): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1794)
05-15 00:08:18.415: E/AndroidRuntime(4547): ... 11 more
05-15 00:08:18.415: E/AndroidRuntime(4547): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has an empty constructor that is public
05-15 00:08:18.415: E/AndroidRuntime(4547): at android.support.v4.app.Fragment.instantiate(Fragment.java:395)
05-15 00:08:18.415: E/AndroidRuntime(4547): at android.support.v4.app.Fragment.instantiate(Fragment.java:363)
05-15 00:08:18.415: E/AndroidRuntime(4547): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:264)
05-15 00:08:18.415: E/AndroidRuntime(4547): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
05-15 00:08:18.415: E/AndroidRuntime(4547): ... 20 more
05-15 00:08:18.415: E/AndroidRuntime(4547): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.MapFragment in loader dalvik.system.PathClassLoader[/data/app/org.rtesh.onemore-1.apk]
05-15 00:08:18.415: E/AndroidRuntime(4547): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
05-15 00:08:18.415: E/AndroidRuntime(4547): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
05-15 00:08:18.415: E/AndroidRuntime(4547): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
05-15 00:08:18.415: E/AndroidRuntime(4547): at android.support.v4.app.Fragment.instantiate(Fragment.java:385)
05-15 00:08:18.415: E/AndroidRuntime(4547): ... 23 more
我的MainActivity类是
@SuppressLint("NewApi")
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;
@SuppressLint("NewApi")
@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)));
}
}
}
和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>
我在maifest类中提供了适当的权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.rtesh.onemore"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="org.rtesh.android.locationapi.maps.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="com.org.rtesh.android.locationapi.maps.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" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="org.rtesh.onemore.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="mykey" />
</application>
</manifest>
我引用了谷歌播放服务库为什么我面临这个错误?请一些帮助 并请指出谷歌地图版本1和2之间的主要区别!!
答案 0 :(得分:2)
尝试用以下内容替换您的布局:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
您需要在11下使用SupportMapFragment
API!
在您的MainActivity中(或者您希望放置v2映射的每个活动中),您必须扩展FragmentActivity或Activity,或者您要使用SupportMapFragment或仅使用MapFragment。仅在定位API 12及更高版本时才使用MapFragment类。否则,请使用SupportMapFragment。这是一个微妙的差异,但它会产生错误。
编辑:
请在FragmentActivity中更改您的代码:
map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
理解这个概念。由于您的目标是不支持片段的较低Android版本,因此您需要使用支持片段来处理片段。因此,无论您在何处使用片段,都要确保从support
库中导入片段。
答案 1 :(得分:1)
首先,与此帖的其他响应者一样,您应该将最低Android SDK级别更改为至少11
android:minSdkVersion="11"
这是我昨天做的一个工作示例。
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:name="com.google.android.gms.maps.SupportMapFragment"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraTargetLat="33.41662287712097"
map:cameraTargetLng="35.21843892856462"
map:cameraZoom="4"/>
</LinearLayout>In MainActivity.java onCreate()
在MainActivity.java onCreate()
中public class MainActivity extends FragmentActivity {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
GoogleMap mMap = mapFragment.getMap();
mMap.addMarker(new MarkerOptions()
.position(new LatLng(35.21843892856462, 33.41662287712097))
.title("My Office"));
}
要查看代码结果的截图,请参阅此帖子中的答案。 gl hf! Google Map V2 not working for me . Any sample?
答案 2 :(得分:1)
公共类SearchOffer扩展了FragmentActivity {
GoogleMap gmpSearchOfferMap;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.search_offers);
if (gmpSearchOfferMap == null) {
gmpSearchOfferMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.gmpOfferMap)).getMap();
}
if (gmpSearchOfferMap != null) {
gmpSearchOfferMap.setMyLocationEnabled(true);
gmpSearchOfferMap.getUiSettings().setMyLocationButtonEnabled(
true);
gmpSearchOfferMap.getUiSettings().setZoomControlsEnabled(true);
gmpSearchOfferMap.getUiSettings().setZoomGesturesEnabled(true);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
答案 3 :(得分:0)
你的min sdk是8.你需要为12以下的api版本添加支持库
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
因此,为了向后兼容,您需要添加支持库
android:name="com.google.android.gms.maps.SupportMapFragment "
由于您没有添加支持库,因此您获得了classcastexception
要添加支持库,请查看以下链接
http://developer.android.com/tools/extras/support-library.html
下载支持片段
http://developer.android.com/tools/extras/support-library.html#Downloading
有关更多信息,请查看链接
检查标题开发人员指南上方的行。
有关map api v2的更多信息,请查看文档