Error while running Google maps API V2 example
It might have caused because the library was not imported correctly, follow the steps in this link
If that didn't resolve and you have set up the library correctly, this may be because proguard might have stripped away play services. To avoid it add the following in proguard-project.txt
-keep class * extends java.util.ListResourceBundle {
protected Object[][] getContents();
}
-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
public static final *** NULL;
}
-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
@com.google.android.gms.common.annotation.KeepName *;
}
-keepnames class * implements android.os.Parcelable {
public static final ** CREATOR;
}
- Your suggested Android manifest fixes
You might not have added the key, I see 'API_KEY' string instead of the key, it should be something like this(obviously the key will be different)
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AZzaSyDhkhNotUseFullKey49ylKD2bw1HM" />
You are missing these in the AndroidManifest.xml,
<permission
android:name="com.juliette.basicgooglemap.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.juliette.basicgooglemap.permission.MAPS_RECEIVE" />
Add this too:
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
Next setup your layout and code like this post, you are not using the fragment properly.
Saving MapFragment (Maps v2) State in Android
How can I accomplish this?
If you replace()
a fragment, the old fragment's views are destroyed, which takes out your MapView
and, presumably, the CameraPosition
.
onSaveInstanceState()
is mostly for configuration changes, such as screen rotations. MapFragment
and SupportMapFragment
already retain the CameraPosition
(which, BTW, is Parcelable
, so you can save the whole object in the Bundle
rather than piecemeal).
You could consider using show()
and hide()
instead of replace()
, so the fragment and its views sticks around.
MapFragment in Fragment, alternatives?
Use MapView instead of MapFragment in your Fragment's layout. Remember to call MapView's lifecycle methods:
- onCreate(Bundle)
- onResume()
- onPause()
- onDestroy()
- onSaveInstanceState(Bundle)
- onLowMemory()
as described here.
Btw. you shouldn't be using MapFragment, only SupportMapFragment and support library.
Edit:
If you switch to support library, you can use code from comment #1 here:
http://code.google.com/p/gmaps-api-issues/issues/detail?id=5064#c1
How to receive GPS Location and inflate Fragment MapView to programmatically add markers with SyncAdapter
After desperate attempts I found the solution given in a similar thread by Mrigank here
I believe after my tweak in the code even though it is being inflated the parent container is somehow null.
So My yelp_google_map.xml goes like this.
<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="mem.edu.joshua.MapsActivity"
android:background="#009688">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
I got rid of the the @Override public void onDestroyView part
onCreateView stays like this
top variables:
private SupportMapFragment sMap;
private FragmentManager fm;
private GoogleMap mMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getLoaderManager().initLoader(CURSOR_LOADER_ID, null, this);
View v = inflater.inflate(R.layout.yelp_google_map, container, false);
fm=getChildFragmentManager();
sMap = ((SupportMapFragment) fm.findFragmentById(R.id.map));
mMap = sMap.getMap();
initQueryCursor = getActivity().getContentResolver().query(QuoteProvider.Quotes.CONTENT_URI,
new String[]{QuoteColumns.LATITUDE, QuoteColumns.LOGITUDE, QuoteColumns.ID_BUSINESS_NAME}, null,
null, null);
LatLngBounds.Builder builder = new LatLngBounds.Builder();
if (initQueryCursor != null) {
DatabaseUtils.dumpCursor(initQueryCursor);
initQueryCursor.moveToFirst();
for (int i = 0; i < initQueryCursor.getCount(); i++) {
home = new LatLng(Double.valueOf(initQueryCursor.getString(initQueryCursor.getColumnIndex("latitude"))),
Double.valueOf(initQueryCursor.getString(initQueryCursor.getColumnIndex("longitude"))));
mMap.addMarker(new MarkerOptions().position(home).
title(initQueryCursor.getString(initQueryCursor.getColumnIndex("id_bussines_name"))));
initQueryCursor.moveToNext();
builder.include(home);
}
if(home!=null) {
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 91, 91, 42));
}
}
return v;
}
Added all this in the same Fragment java file
@Override
public void onResume() {
super.onResume();
sMap.onResume();
}
@Override
public void onPause() {
super.onPause();
sMap.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
sMap.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
sMap.onLowMemory();
}
Lastly in the mainActivity.java
getSupportFragmentManager()
.beginTransaction()
.add(R.id.map, new MapsActivity(), MapsActivity.LOG_TAG)
.disallowAddToBackStack()
.commitAllowingStateLoss();
Hope this helps somebody. It was a nightmare for me. My application is now working perfectly.
Error inflating Map with fragment
Try deleting all nested fragments in onDestroyView()
and inflating them in onCreateView
like this answer suggests.
Android app error - Duplicate id 0x7f04000f, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
Fixed it with the following code in my map fragment:
public void onDestroyView() {
super.onDestroyView();
FragmentManager fm = getActivity().getSupportFragmentManager();
Fragment fragment = (fm.findFragmentById(R.id.map));
FragmentTransaction ft = fm.beginTransaction();
ft.remove(fragment);
ft.commit();
}
Related Topics
Run Code Only Once After an Application Is Installed on Android Device
Android Play Services 6.5: Locationclient Is Missing
Receive Push Notifications on One Android App from Two Firebase Projects
How to Use and Style New Alertdialog from Appcompat 22.1 and Above
Android Room: Insert Relation Entities Using Room
How to Check If Receiver Is Registered in Android
How to Implement the Material-Design Elevation for Pre-Lollipop
Textinputlayout Not Showing Edittext Hint Before User Focus on It
Android:Fill Spinner from Java Code Programmatically
Popupwindow $Badtokenexception: Unable to Add Window -- Token Null Is Not Valid
Install_Failed_Shared_User_Incompatible While Using Shared User Id
What's the Difference Between Setwebviewclient VS. Setwebchromeclient
Application Installation Failed in Android Studio
How to Set Cursor Position in Edittext
Does the Android Art Runtime Have the Same Method Limit Limitations as Dalvik