【问题标题】:Stop working google map in fragment android停止在片段android中工作谷歌地图
【发布时间】:2013-12-28 03:09:02
【问题描述】:

我是安卓谷歌地图的新手。我刚刚在 Fragment 中尝试了简单的谷歌地图,但我停止了工作。也许这个问题与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)

【问题讨论】:

标签: android google-maps android-fragments


【解决方案1】:

如果那只是你的 xml,那么你就不需要那个 RelativeLayout。 如果您使用android:name="com.google.android.gms.maps.MapFragment",则实际上不需要class="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 服务一起使用。

【讨论】:

  • 我现在没有使用真机。我只是使用avd模拟器。顺便说一句,如何在我的模拟器中配置 Google Play 服务?
  • 从Android 4.2.2开始,配置更简单。只需要有一个运行“Google API 4.2.2”或更高版本(不是 Android 4.2.2)的模拟器。更多信息在这里:developer.android.com/google/play-services/setup.html
猜你喜欢
  • 2015-09-12
  • 1970-01-01
  • 1970-01-01
  • 2017-08-16
  • 2020-03-14
  • 1970-01-01
  • 2013-12-10
  • 1970-01-01
  • 2016-01-10
相关资源
最近更新 更多