android-无法实例化片段谷歌地图api 2

时间:2014-05-09 07:11:12

标签: android google-maps android-fragments google-maps-api-2 android-support-library

这些是我的代码,我正在为android api 8+编程。我想在地图上显示位置,我正在使用谷歌地图api 2,我已经导入了google-play-services_lib并且jar文件退出。我有一个滑动选项卡,它包含3个片段。这是我的代码

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);

}
    }

清单:

  <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>

这是错误:

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

别的,这就是我导入google play服务的方式,我将google-play-services作为项目导入我的工作区,然后将其作为库添加到我的项目中

你能帮我吗? 谢谢

1 个答案:

答案 0 :(得分:0)

请尝试此代码。这段代码正在我这边工作。

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

并将此代码用于布局文件。

<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>