2013-05-31 49 views
0

我是Android編程的新手,我已經運行了Android開發人員指南和YouTube指南,並且還檢查了其他類似的問題在這裏提出,但我仍然無法在AVD模擬器上加載地圖。我還引進谷歌播放,services_lib和 的Android構建目標的Android 4.2.2 API 17Google Maps API 2 for Android「不幸的是,[app]已停止。」無法運行應用程序

我能夠在仿真器,但每一次成功安裝我的應用程序,當我嘗試運行它,它會通知「不幸的是,[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); 
} 
} 

logcat的

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

請不要提供您的API密鑰.. –

+0

@JaiKumar現在這並不重要 –

+1

你會發表您的logcat –

回答

0

谷歌Play服務目前無法使用虛擬設備的,雖然有點谷歌搜索的,它看起來像有辦法一輪它。

最好的辦法是隻使用一個真正的設備,如果你有一個可用,並確保你安裝谷歌播放服務:)

+0

噢好的...謝謝....但你能告訴我,如果我需要在我的代碼做任何改變? – user2439492

+0

我認爲你應該使用片段類。你可能需要閱讀,因爲我不完全理解碎片。 如果它不適用於真實設備,我會說要麼檢查您的片段,要麼Google Play服務沒有正確設置 希望你能把它分類:) – kyul

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 =新的MarkerOptions()位置(新的經緯度(緯度,經度)); markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)); ()新的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 

    } 
相關問題