我是Android的谷歌地图新手。我只是在片段中尝试了简单的谷歌地图,我停止了工作。也许这个问题与this相同,但我没有找到好的答案。我希望有一个人可以帮助我。 这个java代码
public class MenujuStasiun extends Fragment {
public MenujuStasiun() {
}
private GoogleMap googleMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.mstasiun, container, false);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
return rootView;
}
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getActivity(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
@Override
public void onResume() {
super.onResume();
initilizeMap();
}
}
这个mstasiun.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/map"
class="com.google.android.gms.maps.MapFragment"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
这是logcat错误
12-27 23:36:28.336: E/AndroidRuntime(1310): android.view.InflateException: Binary XML file line #6: Error inflating class fragment
12-27 23:36:28.336: E/AndroidRuntime(1310): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
12-27 23:36:28.336: E/AndroidRuntime(1310): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
12-27 23:36:28.336: E/AndroidRuntime(1310): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
12-27 23:36:28.336: E/AndroidRuntime(1310): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
12-27 23:36:28.336: E/AndroidRuntime(1310): at com.tugasbesar.medantrain.MenujuStasiun.onCreateView(MenujuStasiun.java:21)
12-27 23:36:28.336: E/AndroidRuntime(1310): at android.app.Fragment.performCreateView(Fragment.java:1700)
12-27 23:36:28.336: E/AndroidRuntime(1310): at android.app.Activity.onCreateView(Activity.java:4777)
12-27 23:36:28.336: E/AndroidRuntime(1310): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
答案 0 :(得分:0)
如果那只是你的xml,那么你真的不需要那个RelativeLayout。
如果您使用class="com.google.android.gms.maps.MapFragment"
,则实际上不需要android:name="com.google.android.gms.maps.MapFragment"
。你缺少的是xmlns:map="http://schemas.android.com/apk/res-auto"
汇总:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
您正在使用真实设备进行测试,对吗?您需要配置模拟器才能使其与Google Play服务配合使用。