0

I am new in google map for android. I just tried simple google map in Fragment and i got stop working. Maybe this Question same with this but i have not found good answer. I hope someone can help me. This java code

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();
    }

}

this 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>

this is the logcat error

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)
Community
  • 1
  • 1

1 Answers1

0

If that's only your xml, then you don't really need that RelativeLayout. You don't really need class="com.google.android.gms.maps.MapFragment" if you use android:name="com.google.android.gms.maps.MapFragment". What you are missing is xmlns:map="http://schemas.android.com/apk/res-auto"

Summarizing:

<?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" />

You are using a real device to test, right? You need to configure your emulator in order to make it work with Google Play Services.

gian1200
  • 3,670
  • 2
  • 30
  • 59
  • I am not using real device now. I just use avd emulator. Btw how to configure Google Play Services in my emulator ? – user2967801 Dec 28 '13 at 17:51
  • Starting from Android 4.2.2, it is easier to configure. Just need to have an emulator running "Google API 4.2.2" or higher (not Android 4.2.2). More info here: http://developer.android.com/google/play-services/setup.html – gian1200 Dec 28 '13 at 18:31