0

I want to show map in activity by using viewPager but I am still getting rendering problem in xml file.

A fragment tag allows a layout file to dynamically include different layouts at runtime. At layout editing time the specific layout to be used is not known. You can choose which layout you would like previewed while editing the layout. - 

(Pick Layout...)

Do not warn about tags in this session

add_masjid.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText1"
        android:layout_alignBottom="@+id/editText1"
        android:layout_alignParentStart="true"
        android:text="@string/xyz"
        android:textColor="#FFFFFF"
        android:textSize="20sp"
        />

    <Button
        android:id="@+id/generalId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/rect1"
        android:onClick="geoLocate"
        android:text="@string/abc"
        android:textColor="#FFFFFF"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/generalId"
        android:layout_alignBottom="@+id/generalId"
        android:layout_toEndOf="@+id/textView1"
        android:layout_toStartOf="@+id/generalId"
        android:ems="10"
        android:inputType="text"
        android:labelFor="@+id/editText1" />

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/generalId" />

</RelativeLayout>

It is also creating an error in Logcat

FATAL EXCEPTION: main
Process: com.example.saroosh.masjidnow, PID: 9220
android.view.InflateException: Binary XML file line #44: Error inflating class fragment
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:770)
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
   at com.example.saroosh.masjidnow.Tabs.AddMasjid.onCreateView(AddMasjid.java:26)
   at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080)
   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)

AddMasjid.java

package com.example.saroosh.masjidnow.Tabs;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.saroosh.masjidnow.R;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;

public class AddMasjid extends Fragment {

    MapView gMapView;
    GoogleMap gMap = null;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v =inflater.inflate(R.layout.add_masjid,container,false);

//        gMapView = (MapView) v.findViewById(R.id.map);
//        gMapView.getMapAsync(this);
//        gMapView.onCreate(savedInstanceState);
//        gMapView.onResume(); // needed to get the map to display immediately
//        try {
//            MapsInitializer.initialize(getActivity().getApplicationContext());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        return v;
    }

//    @Override
//    public void onMapReady(GoogleMap map) {
//        gMap = map;
//        gMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
//        gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new
//                LatLng(49.39,-124.83), 20));
//    }
//    @Override
//    public void onResume() {
//        super.onResume();
//        gMapView.onResume();
//    }
//
//    @Override
//    public void onPause() {
//        super.onPause();
//        gMapView.onPause();
//    }
//
//    @Override
//    public void onDestroy() {
//        super.onDestroy();
//        gMapView.onDestroy();
//    }
//
//    @Override
//    public void onLowMemory() {
//        super.onLowMemory();
//        gMapView.onLowMemory();
//    }


}

Please tell me how can I fix this problem.

nabia saroosh
  • 399
  • 1
  • 14
  • 36

2 Answers2

0

Use com.google.android.gms.maps.SupportMapFragment instead of com.google.android.gms.maps.MapView

<fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/generalId" />
Naveen Kumar M
  • 7,497
  • 7
  • 60
  • 74
0

I have solved my problem by adding below code in xml instead of fragment.

add_masjid.xml

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

</com.google.android.gms.maps.MapView>

instead of

<fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/generalId" />

or this code in java also.

package com.example.saroosh.masjidnow.Tabs;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.saroosh.masjidnow.R;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;

public class AddMasjid extends Fragment implements OnMapReadyCallback{

    MapView gMapView;
    GoogleMap gMap = null;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v =inflater.inflate(R.layout.add_masjid,container,false);

        gMapView = (MapView) v.findViewById(R.id.map);
        gMapView.getMapAsync(this);
        gMapView.onCreate(savedInstanceState);
        gMapView.onResume(); // needed to get the map to display immediately
        try {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return v;
    }

    @Override
    public void onMapReady(GoogleMap map) {
        gMap = map;
        gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new
                LatLng(0,0), 0));
    }
    @Override
    public void onResume() {
        super.onResume();
        gMapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        gMapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        gMapView.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        gMapView.onLowMemory();
    }


}
nabia saroosh
  • 399
  • 1
  • 14
  • 36