Skip to content

Instantly share code, notes, and snippets.

@rahulcn
Last active January 11, 2016 09:04
Show Gist options
  • Save rahulcn/2a20f4a9e8f4e41b7efd to your computer and use it in GitHub Desktop.
Save rahulcn/2a20f4a9e8f4e41b7efd to your computer and use it in GitHub Desktop.
<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"
tools:context=".views.MainActivity$PlaceholderFragment">
<fragment
android:id="@+id/mapFragment"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
01-11 14:32:54.759 13373-13373/com.chitchat.android.chitchat E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.chitchat.android.chitchat, PID: 13373
android.view.InflateException: Binary XML file line #8: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at com.chitchat.android.chitchat.views.MapFragment.onCreateView(MapFragment.java:45)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1426)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:728)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:552)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:514)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:495)
at android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:1778)
at android.support.design.widget.TabLayout.selectTab(TabLayout.java:871)
at android.support.design.widget.TabLayout.selectTab(TabLayout.java:841)
at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1115)
at android.support.design.widget.TabLayout$1.onClick(TabLayout.java:665)
at android.view.View.performClick(View.java:4789)
at android.view.View$PerformClick.run(View.java:19881)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5294)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
Caused by: java.lang.IllegalArgumentException: Binary XML file line #8: Duplicate id 0x7f0e00c1, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.SupportMapFragment
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2289)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:177)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
at com.chitchat.android.chitchat.views.MapFragment.onCreateView(MapFragment.java:45) 
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1426) 
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:728) 
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613) 
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570) 
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) 
at android.support.v4.view.ViewPager.populate(ViewPager.java:1106) 
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:552) 
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:514) 
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:495) 
at android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:1778) 
at android.support.design.widget.TabLayout.selectTab(TabLayout.java:871) 
at android.support.design.widget.TabLayout.selectTab(TabLayout.java:841) 
at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1115) 
at android.support.design.widget.TabLayout$1.onClick(TabLayout.java:665) 
at android.view.View.performClick(View.java:4789) 
at android.view.View$PerformClick.run(View.java:19881) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5294) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 
package com.chitchat.android.chitchat.views;
import android.app.Dialog;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.chitchat.android.chitchat.R;
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.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
/**
* Created by Rahul Chandra on 12/15/15.
*/
public class MapFragment extends Fragment
implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
GoogleMap mMap;
private static final int ERROR_DIALOG_REQUEST = 9001;
private static final double
PES_LAT = 12.934131,
PES_LNG = 77.536756;
private GoogleApiClient mLocationClient;
private LocationListener mLocationListner;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_map, container, false);
if (servicesOK()) {
if (initMap()) {
gotoLocation(PES_LAT, PES_LNG, 15);
// Enabling my current location button
// mMap.setMyLocationEnabled(true);
mLocationClient = new GoogleApiClient.Builder(getActivity())
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mLocationClient.connect();
return rootView;
} else {
Toast.makeText(getActivity(), "Map not connected!", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(container.getContext(), "Services not ok!", Toast.LENGTH_SHORT).show();
//setContentView(R.layout.activity_main);
}
return null;
}
public boolean servicesOK() {
int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity());
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
} else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, getActivity(), ERROR_DIALOG_REQUEST);
dialog.show();
} else {
Toast.makeText(getActivity(), "Can't connect to mapping service", Toast.LENGTH_SHORT).show();
}
return false;
}
private boolean initMap() {
if (mMap == null) {
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapFragment);
mMap = mapFragment.getMap();
}
return (mMap != null);
}
private void gotoLocation(double lat, double lng, float zoom) {
LatLng latLng = new LatLng(lat, lng);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latLng, zoom);
mMap.animateCamera(update);
}
@Override
public void onConnected(Bundle bundle) {
Toast.makeText(getActivity(), "Ready to Map!", Toast.LENGTH_SHORT).show();
mLocationListner = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Toast.makeText(getActivity(), "Location changed: " + location.getLatitude() + ", " + location.getLongitude(), Toast.LENGTH_SHORT).show();
gotoLocation(location.getLatitude(), location.getLongitude(), 15);
}
};
LocationRequest request = LocationRequest.create();
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
request.setInterval(60000);
request.setFastestInterval(30000);
LocationServices.FusedLocationApi.requestLocationUpdates(mLocationClient, request, mLocationListner);
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
@Override
public void onPause() {
super.onPause();
LocationServices.FusedLocationApi.removeLocationUpdates(mLocationClient, mLocationListner);
}
}
package com.chitchat.android.chitchat.adapters;
/**
* Created by Rahul Chandra on 12/15/15.
*/
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import com.chitchat.android.chitchat.views.CheeseListFragment;
import com.chitchat.android.chitchat.views.FriendsFragment;
import com.chitchat.android.chitchat.views.InboxFragment;
import com.chitchat.android.chitchat.views.MapFragment;
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class TabsPagerAdapter extends FragmentPagerAdapter {
public static FragmentManager fragmentManager;
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
fragmentManager = fm;
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
switch (position) {
case 0:
return new CheeseListFragment();
case 1:
return new CheeseListFragment();
case 2:
return new MapFragment();
}
return null;
}
@Override
public int getCount() {
return 3; // Show 3 total pages.
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "TAB 1";
case 1:
return "TAB 2";
case 2:
return "TAB 3";
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment