Reputation: 5757
I am using GoogleMap
in android app using Android Studio
. It was working fine and don't know why suddenly it stopped working. minSdkVersion is 15 and targetSdkVersion is 24. Please check my below code and help me to come out of this :
XML:
<FrameLayout 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">
<fragment
android:id="@+id/mapFragment"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
Gridle:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.android.support:design:24.2.0'
compile 'com.google.android.gms:play-services:9.4.0'
compile 'com.google.maps.android:android-maps-utils:0.4.3'
compile 'com.loopj.android:android-async-http:1.4.9'
compile 'com.google.code.gson:gson:2.7'
compile 'com.googlecode.android-query:android-query:0.24.3'
compile 'com.makeramen:roundedimageview:2.2.1'
}
Java:
public class MapsActivity extends Fragment implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.mapFragment);
}
}
Manifest:
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/google_maps_key" />
Error:
It is giving error in xml at <fragment
, error: android.view.InflateException: Binary XML file : Error inflating class fragment
Upvotes: 0
Views: 3164
Reputation: 391
If it crashes second time, you need to destroy it.
public void onDestroy() {
super.onDestroy();
getFragmentManager().beginTransaction().remove(mapfragmentnamehere).commit();
}
Upvotes: 0
Reputation: 5757
After adding permission in manifest, It solved my issue. Check below code :
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="24" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="24" />
Upvotes: 4
Reputation: 3708
getChildFragmentManager
is used in Nested Fragments. The fragment you've used is not a child of another fragment. Use getSupportFragmentManager
or getFragmentManager
for fragment transactions.
Take a look at this question for more information.
In addition, I would like to mention that You're integrating an old Maps API. Try the latest one https://developers.google.com/maps/documentation/android-api/map
Upvotes: 0
Reputation: 5550
In your onCreate
add:
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
In XML add context
:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.___yourPackageName______"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/mapFragment"
class="com.google.android.gms.maps.SupportMapFragment"
tools:context="com.___yourPackageName______"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
Upvotes: 0
Reputation: 454
public void onMapReady(GoogleMap googleMap) {
setUpMap(googleMap);
}
Upvotes: -1
Reputation: 454
Please use getMapAsync.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this)
Upvotes: 0