E/AndroidRuntime(8592):由以下原因引起:android.view.InflateException:二进制XML文件行#2:膨胀类片段时出错
嗯,我正试图将谷歌地图添加到我的应用程序中,但我遇到了这些错误E/AndroidRuntime(8592):由以下原因引起:android.view.InflateException:二进制XML文件行#2:膨胀类片段时出错,android,eclipse,google-maps,android-fragments,Android,Eclipse,Google Maps,Android Fragments,嗯,我正试图将谷歌地图添加到我的应用程序中,但我遇到了这些错误 04-17 16:25:07.169: E/AndroidRuntime(8592): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment 04-17 16:25:07.169: E/AndroidRuntime(8592): Caused by: java.lang.ClassNotFo
04-17 16:25:07.169: E/AndroidRuntime(8592): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
04-17 16:25:07.169: E/AndroidRuntime(8592): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.guide_oran-1.apk]
我的主要活动:
package com.example.guide_oran;
import android.app.Activity;
import android.os.Bundle;
public class Activity_map extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gmap);
}
}
我的xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
class="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
尝试将片段放入布局中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>
你可以找到一个好的教程它可能是由你的
活动
扩展和你只有一个片段
类的布局造成的。您需要使用FragmentActivity
扩展活动,以允许活动处理片段。也就是说,您还需要将fragment类更改为支持SupportMapFragment
public class Activity_map extends FragmentActivity {
GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gmap);
// find the fragment and commit it into this activity
SupportMapFragment mapfragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
// initialization of googlemap
googleMap = mapfragment.getMap();
// It might be resumed by:
// googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
}
}
然后,您需要将片段的类更改为:
android:name="com.google.android.gms.maps.SupportMapFragment"
您可以在:另外,一步一步地找到使用谷歌地图的方法。实际的问题是他使用的是
活动
,而不是碎片活动
(正如下面@Filo所指出的)