0

我是 android 编程的新手,我已经浏览了 android 开发者指南和 YouTube 指南,还检查了这里提出的其他类似问题,但仍然无法在 AVD 模拟器上加载地图。我还导入了 google-play-services_lib 和 Android 构建目标Android 4.2.2 API 17

我能够在模拟器上成功安装我的应用程序,但每次我尝试运行它时,它都会发出“不幸的是,[app] 已停止”的通知。

任何帮助的尝试将不胜感激。这是我的代码:

manifest.xml 文件:

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

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="17" />
<permission
    android:name="com.android.sitemarker.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />
 <uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />
<uses-permission android:name="com.android.sitemarker.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />                                                                                                                                
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.android.sitemarker.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="xxxx-xxxx-xxxxx" />
   </application>

   </manifest>

main.xml 文件:

 <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=".MainActivity" >
  <fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.MapFragment" />




    </RelativeLayout>

MainActivity.java 文件:

package com.android.sitemarker;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
}

日志猫

05-31 08:33:33.989: E/AndroidRuntime(2125):     ... 24 more
4

2 回答 2

0

Google Play 服务目前不适用于虚拟设备,尽管通过谷歌搜索,似乎有办法绕过它。

最好的办法是在有可用设备的情况下使用真实设备,并确保安装 Google Play 服务 :)

于 2013-05-31T10:39:14.590 回答
0

谷歌地图在真实设备上运行,它不会在模拟器上运行,所以如果它不能在真实设备上运行,请在真实设备上尝试,然后尝试此代码,您还可以正确检查您是否在项目属性中添加了 googleplay 库

public class MainActivity extends Activity implements LocationListener{
LocationManager locationManager;
private GoogleMap googleMap;
double latitude=17.385044;
double longitude=78.486671;
private String provider;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 LocationManager service=(LocationManager)getSystemService(LOCATION_SERVICE);
boolean enableGPS= service.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean enableWifi=service.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
 // Check if enabled and if not send user to the GSP settings
    // Better solution would be to display a dialog and suggesting to 
     // go to the settings
 if(!enableGPS){
     Toast.makeText(getApplicationContext(),"Gps signal not found",1).show();
    Intent intent=new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    startActivity(intent);
}
locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
 Criteria criteria = new Criteria();
 provider = locationManager.getBestProvider(criteria, false);
 Location location = locationManager.getLastKnownLocation(provider);

 // Initialize the location fields
 if (location != null) {
     Toast.makeText(this, "Selected Provider " + provider,
             Toast.LENGTH_SHORT).show();
     onLocationChanged(location);
 } else {

     //do something
 }


try {
    //loading map
    initilizeMap();
} catch (Exception e) {
    // TODO: handle exception
e.printStackTrace();
}
}
   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",0).show();  
    }
}

MarkerOptions markerOptions=new MarkerOptions().position(new LatLng(latitude, longitude)); 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();
 }

/* Remove the locationlistener updates when Activity is paused */
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}

 public void onLocationChanged(Location location) {
double lat =  location.getLatitude();
double lng = location.getLongitude();
Toast.makeText(this, "Location " + lat+","+lng,
        Toast.LENGTH_LONG).show();
LatLng coordinate = new LatLng(lat, lng);
Toast.makeText(this, "Location " + coordinate.latitude+","+coordinate.longitude,
        Toast.LENGTH_LONG).show();
Marker startPerc = googleMap.addMarker(new MarkerOptions()
.position(coordinate)
.title("Start")
.snippet("Inizio del percorso")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));
}


  public void onProviderDisabled(String provider) {
  Toast.makeText(this, "Enabled new provider " + provider,
        Toast.LENGTH_SHORT).show();

   }


  public void onProviderEnabled(String provider) {
  Toast.makeText(this, "Disabled provider " + provider,
        Toast.LENGTH_SHORT).show();

     }


public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

  }
于 2013-10-16T13:41:18.260 回答