1

I am trying to diaplay a google map in a thumbnail sized dialog box using DialogFragment.

But i get the following eroor :- Binary file XML line #25: Error inflating class fragment

and the application crashes as soon as it starts. I searched a lot of questions in SO but was unable to find a solution.

This is my code:-

Main XMl file:-

    <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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Spinner
        android:id="@+id/spr_place_type"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_alignParentTop="true" />

    <Button
        android:id="@+id/btn_find"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@id/spr_place_type"
        android:text="@string/str_btn_find" />

<com.google.android.gms.maps.MapView
    android:id="@+id/map"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/spr_place_type"
   />

</RelativeLayout>

Java class:-

    public class PlaceDialogFragment extends android.support.v4.app.DialogFragment
    {
      @Override
        public void onCreate(Bundle savedInstanceState)
        {

            // For retaining the fragment on screen rotation
            setRetainInstance(true);
            super.onCreate(savedInstanceState);



        }




        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                Bundle savedInstanceState) 
        {

             View v = inflater.inflate(R.layout.dialog_layout, container, false);

    MapView m = (MapView) v.findViewById(R.id.map);
    m.onCreate(savedInstanceState);



            // Getting reference to ViewFlipper
            mFlipper = (ViewFlipper) v.findViewById(R.id.flipper);

            // Getting reference to TextView to display photo count
            mTVPhotosCount = (TextView) v.findViewById(R.id.tv_photos_count);

            // Getting reference to TextView to display place vicinity
            mTVVicinity = (TextView) v.findViewById(R.id.tv_vicinity);

            if(mPlace!=null){

                // Setting the title for the Dialog Fragment
                getDialog().setTitle(mPlace.mPlaceName);

                // Array of references of the photos
                Photo[] photos = mPlace.mPhotos;

                // Setting Photos count
                mTVPhotosCount.setText("Photos available : " + photos.length);

                // Setting the vicinity of the place
                mTVVicinity.setText(mPlace.mVicinity); 




                // Creating an array of ImageDownloadTask to download photos
                ImageDownloadTask[] imageDownloadTask = new ImageDownloadTask[photos.length];

}

mainActivity.java

 SupportMapFragment fragment = ( SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

        // Getting Google Map
        mGoogleMap = fragment.getMap();

        // Enabling MyLocation in Google Map
        mGoogleMap.setMyLocationEnabled(true);

Error:-

01-09 13:33:00.509: E/AndroidRuntime(8393): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.locations/com.example.locations.MainActivity}: java.lang.NullPointerException

01-09 13:33:00.509: E/AndroidRuntime(8393): Caused by: java.lang.NullPointerException 01-09 13:33:00.509: E/AndroidRuntime(8393): at com.example.locations.MainActivity.onCreate(MainActivity.java:119)

user3146095
  • 419
  • 2
  • 7
  • 25
  • @Raghunandan I tried removing that and still get the error. Also In the graphical layout i get the error :- Pick preview layout from fragment layout context menu. Can u please give me an example on how to use mapView in a fragment – user3146095 Jan 09 '14 at 05:51
  • http://stackoverflow.com/questions/20919048/android-android-view-inflateexception-binary-xml-file-line-8-error-inflatin/20971284#20971284 – Raghunandan Jan 09 '14 at 05:53
  • @Raghunandan I have edited my code with changes. Now i can see the basic app layout but cant see any items on my spinner – user3146095 Jan 09 '14 at 06:11
  • check the spinner and the layout. place your other views below the spinner relative to it – Raghunandan Jan 09 '14 at 06:44
  • @Raghunandan I managed to solve the spinner problem but again i get an issue with the map fragment. I used map view instead of fragment. But later in my main activity i need to do something like this :- ** SupportMapFragment fragment = ( SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); // Getting Google Map mGoogleMap = fragment.getMap(); // Enabling MyLocation in Google Map mGoogleMap.setMyLocationEnabled(true);** – user3146095 Jan 09 '14 at 08:12
  • what you are doing is wrong. You need to do all map customization in fragment. Coz your fragment has mapview. `MapView m = (MapView) v.findViewById(R.id.map);` then `GoogleMap map = m.getMap()`. then do what is required – Raghunandan Jan 09 '14 at 08:16
  • pls re-read my previous comment – Raghunandan Jan 09 '14 at 08:19
  • @Raghunandan I am not sure if i can place that much code in my fragment class. I have referred this link http://wptrafficanalyzer.in/blog/showing-nearby-places-with-photos-at-any-location-in-google-maps-android-api-v2/ – user3146095 Jan 09 '14 at 08:28
  • why not? it will work. check the link i posted at the start. what ever customization you do on map object should be in fragment. There is a lot of code in that link. You need to carefully manage them – Raghunandan Jan 09 '14 at 08:29
  • @Raghunandan. Thanks for your time.. Ill check out and post back – user3146095 Jan 09 '14 at 08:35
  • @Raghunandan Is there a possibility Im using an older google maps api .Iam using google maps api v2 – user3146095 Jan 09 '14 at 09:26
  • google maps api v2 is the latest for android as far as i know. You could re check with the docs – Raghunandan Jan 09 '14 at 09:45

0 Answers0