title: Unable to start activity ComponentInfo{Activity}: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
body: <p>I am developing an application which uses google map APIs. In my application i want to show google map and set marker on my current location by getting my device latitude and longitude from GPS. In beginning when i was implement following code that works well, i test it on my device but after installing 15 or 20 times on my mobile device it starts crashing automatically although i didn't do anything on that activity.</p>
  
  <p>The logs which are showing me android studio are following</p>
  
  <p><strong>Logs</strong></p>
  
  <pre><code>09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: FATAL EXCEPTION: main
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: Process: com.example.nabeelfarooq.mapsnavapplication, PID: 9110
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nabeelfarooq.mapsnavapplication/com.example.nabeelfarooq.mapsnavapplication.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2455)
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2517)
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:162)
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:106)
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:189)
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5529)
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at android.view.LayoutInflater.parseInclude(LayoutInflater.java:916)
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at android.view.LayoutInflater.parseInclude(LayoutInflater.java:916)
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at com.example.nabeelfarooq.mapsnavapplication.MainActivity.onCreate(MainActivity.java:59)
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5966)
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2408)
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2517) 
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:162) 
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412) 
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:106) 
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:189) 
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5529) 
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950) 
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745) 
  09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to get length of null array
  </code></pre>
  
  <p><strong>MainActivity</strong></p>
  
  <pre><code>import android.Manifest;
  import android.annotation.TargetApi;
  import android.app.AlertDialog;
  import android.app.Dialog;
  import android.app.FragmentManager;
  import android.content.DialogInterface;
  import android.content.Intent;
  import android.content.pm.PackageManager;
  import android.location.Location;
  import android.location.LocationManager;
  import android.os.Build;
  import android.os.Bundle;
  import android.support.v4.app.ActivityCompat;
  import android.support.v4.content.ContextCompat;
  import android.util.Log;
  import android.view.View;
  import android.support.design.widget.NavigationView;
  import android.support.v4.view.GravityCompat;
  import android.support.v4.widget.DrawerLayout;
  import android.support.v7.app.ActionBarDrawerToggle;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.Toolbar;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.widget.Button;
  import android.widget.Toast;
  
  import com.google.android.gms.common.ConnectionResult;
  import com.google.android.gms.common.GooglePlayServicesUtil;
  import com.google.android.gms.common.api.GoogleApiClient;
  import com.google.android.gms.location.LocationServices;
  import com.google.android.gms.location.LocationSettingsRequest;
  import com.google.android.gms.maps.GoogleMap;
  import com.google.android.gms.maps.OnMapReadyCallback;
  import com.google.android.gms.maps.SupportMapFragment;
  import com.google.android.gms.maps.model.BitmapDescriptorFactory;
  import com.google.android.gms.maps.model.LatLng;
  import com.google.android.gms.maps.model.MarkerOptions;
  
  import java.io.Console;
  
  public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, OnMapReadyCallback, GoogleMap.OnMyLocationChangeListener, GoogleApiClient.OnConnectionFailedListener {
      SupportMapFragment supportMapFragment = SupportMapFragment.newInstance();
      private GoogleMap mMap;
      private GoogleApiClient mGoogleApiClient;
      private Location mLastLocation;
      public static double latitude;
      public static double longitude;
      private Button button;
      private LatlngAdapter databasedata = null;
      final int REQUEST_CODE_ASK_PERMISSIONS = 123;
  
      @TargetApi(Build.VERSION_CODES.M)
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
          button = (Button) findViewById(R.id.button);
          button.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Intent i = new Intent(getApplicationContext(), SearchActivity.class);
                  i.putExtra("latitude", latitude);
                  i.putExtra("longitude", longitude);
                  startActivity(i);
              }
          });
  
          int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
          if (status != ConnectionResult.SUCCESS) {
              int requestCode = 10;
  
              Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
              dialog.show();
          } else {
              SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
              mMap = fm.getMap();
  
              if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                  requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_CODE_ASK_PERMISSIONS);
                  return;
  
              }
              else {
                  if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                          == PackageManager.PERMISSION_GRANTED) {
                      mMap.setMyLocationEnabled(true);
                  }
              }
  
              mMap.setMyLocationEnabled(true);
              mMap.setOnMyLocationChangeListener(this);
          }
          buildGoogleApiClient();
          onMapReady(mMap);
          DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
          ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                  this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
          drawer.setDrawerListener(toggle);
          toggle.syncState();
  
          NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
          navigationView.setNavigationItemSelectedListener(this);
          supportMapFragment.getMapAsync(this);
  
      }
      public void turnGPSOn() {
          LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
          boolean enabled = service
                  .isProviderEnabled(LocationManager.GPS_PROVIDER);
          if (!enabled) {
              AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                      this);
              alertDialogBuilder
                      .setMessage("GPS is disabled in your device. Enable it?")
                      .setCancelable(false)
                      .setPositiveButton("yes",
                              new DialogInterface.OnClickListener() {
                                  public void onClick(DialogInterface dialog,
                                                      int id) {
  /** Here it's leading to GPS setting options*/
                                      Intent callGPSSettingIntent = new Intent(
                                              android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                      startActivity(callGPSSettingIntent);
                                  }
                              });
              alertDialogBuilder.setNegativeButton("No",
                      new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int id) {
                              dialog.cancel();
                          }
                      });
              AlertDialog alert = alertDialogBuilder.create();
              alert.show();
          }
      }
      protected void onStart() {
          super.onStart();
          mGoogleApiClient.connect();
      }
      protected void onPause(){
          super.onPause();
          mGoogleApiClient.disconnect();
      }
      //obtain the supportMapFragment and get notified when the map is ready to be used
      protected synchronized void buildGoogleApiClient(){
  
          if(mGoogleApiClient==null){
              mGoogleApiClient=new GoogleApiClient.Builder(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
          }
      }
  
  
      @Override
      public void onBackPressed() {
          DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
          if (drawer.isDrawerOpen(GravityCompat.START)) {
              drawer.closeDrawer(GravityCompat.START);
          } else {
              super.onBackPressed();
          }
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
  
          //noinspection SimplifiableIfStatement
          if (id == R.id.action_settings) {
              return true;
          }
  
          return super.onOptionsItemSelected(item);
      }
  
      @SuppressWarnings("StatementWithEmptyBody")
      @Override
      public boolean onNavigationItemSelected(MenuItem item) {
          FragmentManager fragmentManager=getFragmentManager();
          android.support.v4.app.FragmentManager fragmentManager1=getSupportFragmentManager();
          // Handle navigation view item clicks here.
          int id = item.getItemId();
  
          if(supportMapFragment.isAdded()) {
              fragmentManager1.beginTransaction().hide(supportMapFragment).commit();
          }
  
          if (id == R.id.nav_home) {
                  //fragmentManager1.beginTransaction().add(R.id.map,supportMapFragment).commit();
              // Handle the home action
          } else if (id == R.id.nav_about_us) {
                  Intent intent=new Intent(this,About_us_Activity.class);
                  startActivity(intent);
  
          } else if (id == R.id.nav_contact_us){
              Intent intent=new Intent(this,Contact_us_Activity.class);
              startActivity(intent);
  
          } else if (id == R.id.nav_Rate_app) {
             Intent intent=new Intent(this,TipsActivity.class);
              startActivity(intent);
  
          }
  
          DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
          drawer.closeDrawer(GravityCompat.START);
          return true;
      }
  
      @Override
      public void onMapReady(GoogleMap googleMap) {
          mMap =googleMap;
          LatLng lahore=new LatLng(latitude,longitude);
          //mMap.addMarker(new MarkerOptions().title("lahore").snippet("lahore HQ").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)).position(sydney));
          //mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
      }
  
      @Override
      public void onMyLocationChange(Location location) {
          latitude=location.getLatitude();
          longitude=location.getLongitude();
          LatLng latLng=new LatLng(latitude,longitude);
          //mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
          //mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
          LatLng lahore=new LatLng(latitude,longitude);
          //mMap.addMarker(new MarkerOptions().title("You are here").snippet("lahore").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)).position(lahore));
         // mMap.moveCamera(CameraUpdateFactory.newLatLng(lahore));
      }
      public void onConnected(Bundle bundle){
          Log.d("CONNECTED", "I am connected");
          mLastLocation=LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
          if(mLastLocation!=null){
              Log.d("Latitude",String.valueOf(mLastLocation.getLatitude()));
              Log.d("Longitude",String.valueOf(mLastLocation.getLongitude()));
          }
      }
  
      @Override
      public void onConnectionFailed(ConnectionResult connectionResult) {
  
      }
      static String latString =Double.toString(latitude);
      static String lonString =Double.toString(longitude);
  }
  </code></pre>
  
  <p><strong>content_main.xml</strong></p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      tools:showIn="@layout/app_bar_main"
      tools:context=".MainActivity"&gt;
  
  
          &lt;fragment xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:id="@+id/map"
              android:layout_alignParentBottom="true"
              tools:context=".MapsActivity"
              android:name="com.google.android.gms.maps.SupportMapFragment"
              tools:layout="@layout/abc_action_bar_up_container"/&gt;
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Get Me Somewhere"
          android:id="@+id/button"
          android:background="#5368ed"
          android:layout_alignParentBottom="true"
          android:layout_centerHorizontal="true"
          android:layout_marginBottom="31dp"
          /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>