0

我正在使用谷歌地图 v2 并收到此错误。

找不到从方法 maps.ag.an.a 引用的类“maps.af.k”

我正确遵守所有规则。我已经创建了 SHA1 指纹并将其添加到带有项目名称的 apikey 的 ANdroid Key

1F:41:29:EC:E8:07:3C:A9:F8:6E:EB:D2:7B:42:41:62:3A:36:CA:F2;com.example.routes

然后在主要文件中使用

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

我还导入了 googleplayservices 并将其正确添加到项目中

我的代码是

public class MainActivity extends FragmentActivity {
GoogleMap googleMap;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    SupportMapFragment fragment = new SupportMapFragment();
    getSupportFragmentManager().beginTransaction()
            .add(android.R.id.content, fragment).commit();
    ;

     googleMap = ((SupportMapFragment)    

  getSupportFragmentManager().findFragmentById(
                R.id.map)).getMap();
  }
}

但我仍然收到那个错误。

4

3 回答 3

0

当您使用SupportMapFragment不需要扩展时,FragmentActivity只需扩展Activity请参阅下面的示例代码:

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;

public class MainActivity extends Activity {

private GoogleMap map;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    map =  ((MapFragment)   getFragmentManager().findFragmentById(R.id.map)).getMap();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

注意:您必须添加 google play services library project 作为对您的项目的引用,您的包名称必须是 com.example.routes 您必须在您的 xml 布局中添加地图片段,而不是在清单中。对您的 Manifest.xml 进行以下更改添加以下权限。

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

添加 OpenGL

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

添加 API 密钥

<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="API_KEY"/>

清单示例:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.package"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name="com.example.package.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="YOUR_API_KEY_HERE"/>
</application>

于 2013-10-16T15:47:46.430 回答
0

尝试扩展 Activity 而不是 FragmentActivity:

  public class MainActivity extends Activity implements LocationListener{
LocationManager locationManager;
    private GoogleMap googleMap;
double lat=17.385044;
double long=78.486671;
 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
    LocationManager service=(LocationManager)getSystemService(LOCATION_SERVICE);
    locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
 Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);
   Location location = locationManager.getLastKnownLocation(provider);
  private void initilizeMap(){
if(googleMap==null){
 googleMap=((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
    //check if map is created succesfully or not
    if(googleMap==null){
Toast.makeText(getApplicationContext(),"sorry unable to create map",1).show();
    }
 } 
MarkerOptions markerOptions=new MarkerOptions().position(new LatLng(lat,long));
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher));
CameraPosition cameraPosition=new CameraPosition.Builder().target(new LatLng(17.385044,     78.486671)).zoom(12).build();
 googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
 googleMap.setMyLocationEnabled(true);
 googleMap.setOnInfoWindowClickListener(null);
 }

  @Override
  protected void onResume(){
 super.onResume();
 initilizeMap();
   }
于 2013-10-16T15:15:50.177 回答
0

我遇到了同样的问题,我的解决方案是再次删除并添加 googleplayservices。

于 2013-10-16T15:48:17.343 回答