0

these are my codes , I'm programming for android api 8+ . I wanna to show the location on the map , I'm using google map api 2 , I've imported google-play-services_lib and the jar file exits . I've a swipe tab and it contains 3 fragment . here are my code

swipe tab Layout 

<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"
tools:context=".Spots_tab2"
android:background="#cccc00"
 >
<fragment 
    android:id="@+id/map"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:name="com.google.android.gsm.maps.SupportMapFragment"
    />

Swipe tab fragment:

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;
import com.google.android.gms.maps.model.MarkerOptions;
public class Spots_tab2 extends Fragment {
private GoogleMap map;
private LatLng location;

public Spots_tab2() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    return inflater.inflate(R.layout.spots_tab2, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    map = ((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    location=  new LatLng(31.15016,51.20188);
    map.addMarker(new MarkerOptions().position(location).title("semirom"));
    CameraUpdate update= CameraUpdateFactory.newLatLng(location);
    update = CameraUpdateFactory.newLatLngZoom(location, 10); // zooom
    map.animateCamera(update);

}
    }

manifest :

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
   <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <permission
    android:name="com.example.maps.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

   <uses-permission android:name="com.example.maps.permission.MAPS_RECEIVE" />

   <uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

   <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat.Light" >
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyAWOjtGwFaWIL4JQqJfrpwoxS1Mx7oMDsk" />
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    </application>

this is the error :

unable to instantiate fragment com.google.android.gsm.SupportMapFragment: make sure class name exists ,is public, and has empty construct or that is public 

something else , this is how I import google play service , I imported google-play-services as a project in my workspace and then add it as a library to my project

Could you please help me thanks

user3579994
  • 131
  • 1
  • 4
  • 12
  • Possible duplicate - http://stackoverflow.com/questions/13719263/unable-instantiate-android-gms-maps-mapfragment – Aditya Gupta May 09 '14 at 07:18
  • move your all code from `onActivityCreted()` to `onCreateView(...)` and try – M D May 09 '14 at 07:18
  • Use class="com.google.android.gms.maps.MapFragment" instead of SupportMapFragment and it should work ;) – Adrien Cerdan May 09 '14 at 07:21
  • @SimplePlan onCreateView for fragment is not a good area , because maybe activity is not loaded yet and some other issues , as the matter of fact ,android don't let me to put these codes in there – user3579994 May 09 '14 at 10:44
  • @AdrienCerdan , could you please tell show me an example ,I tried to put the code and replace it , but it gives error . – user3579994 May 09 '14 at 10:49
  • @AdrienCerdan I get what you mean now , have look at my code , it's the same – user3579994 May 09 '14 at 12:58
  • I solved the problem form here : http://stackoverflow.com/questions/19353255/how-to-put-google-maps-v2-on-a-fragment-using-viewpager – user3579994 May 10 '14 at 04:13

1 Answers1

0

Please try this code. This code is working on my side.

 map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

And Use this code for layout file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"
        android:enabled="true" />

</RelativeLayout>
  • 1
    yes it work for android api11+ , as I said ,I wanna to run it on api8+ . I've a similar code like what you have for api 11 and it works fine. – user3579994 May 09 '14 at 10:41
  • I am having the same issue. This will not work for older API for some reason. I continue to get null pointer exception – portfoliobuilder Sep 16 '15 at 01:09