2016-07-04 14 views
3

Ich möchte eine Anwendung erstellen, die das Google Maps API verwendet, ich begann mit einer einfachen Anwendung, die eine Google Map-Aktivität öffnet. (Ich lade Google Play-Dienste vom SDK-Manager herunter). Ich denke, dass es ein sehr einfaches Programm ist, aber es gibt einen Fehler, wenn ich versuche, meine Anwendung auszuführen.DexIndexOverflowException Mit Google Maps API

Hier ist der Code meines Programms und auch der Fehler von Android Studio generiert.

den Fehler:

Error:Execution failed for task ':app:transformClassesWithDexForDebug'. > com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536

der Code:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {  
private GoogleMap mMap; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps); 
    // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 
} 


/** 
* Manipulates the map once available. 
* This callback is triggered when the map is ready to be used. 
* This is where we can add markers or lines, add listeners or move the camera. In this case, 
* we just add a marker near Sydney, Australia. 
* If Google Play services is not installed on the device, the user will be prompted to install 
* it inside the SupportMapFragment. This method will only be triggered once the user has 
* installed Google Play services and returned to the app. 
*/ 
@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 

    // Add a marker in Sydney and move the camera 
    LatLng sydney = new LatLng(-34, 151); 
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
} 
} 
<resources> 

<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">My API key is here ;) </string> 

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.amine.bea_mapapp"> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 

    <meta-data 
     android:name="com.google.android.geo.API_KEY" 
     android:value="My API key is here ;)" /> 

    <activity 
     android:name=".MapsActivity" 
     android:label="@string/title_activity_maps"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

+0

Entschuldigung für das Fehlerbild, der Fehler ist: Fehler: Ausführung fehlgeschlagen für Task ': app: transformClassesWithDexForDebug'. > com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexIndexOverflowException: Methoden-ID nicht in [0, 0xffff ]: 65536 –

+0

Sehen Sie diesen als Referenz \t http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/ –

+0

Sie können dies tun. http://stackoverflow.com/a/32811133/5296734 Dies kann Ihnen helfen. –

Antwort

0

Internet-Erlaubnis in Manifest-Datei

<uses-permission android:name="android.permission.INTERNET"/> 

auch sicher, dass Sie schalten im Internet und Standort auf Ihrem Telefon hinzufügen

+0

Wie würde dies eine DexIndexOverflowException beheben? –

+0

Code scheint alles gut, hast du API-Schlüssel in Google Map-Aktivität hinzugefügt? – Redman

+0

hast du in dieser Zeile in der Datei google_maps_api.xml den Google-Kartenschlüssel hinzugefügt? YOUR_KEY_HERE geben Sie Ihre generierten Schlüssel an YOUR_KEY_HERE Position sollte es einige Sache beginnt mit aiza – Redman

-1

Ja nach Internet Erlaubnis Hinzufügen versuchen multiDexEnabled wahr zu Ihrer App build.gradle hinzufügen Datei.

defaultConfig { 
    multiDexEnabled true 
} 
+3

höchst zweifelhaft, dass die OP multidex brauchen würde mit so ein kleines Projekt ..... –

+0

Dieser Fehler tritt nur auf, wenn unsere Bibliotheken Konflikt sind und multipleDex Fehler geben. Wenn Sie mehrere Dex nicht aktivieren möchten, überprüfen Sie auch den folgenden Link. http://stackoverflow.com/questions/32798816/unexpected-top-level-exception-com-android-dex-dexexception-multiple-dex-files –

6

Dies ist ein häufiger Fehler, wenn Sie alle Google importieren Play-Dienste, wenn Sie tun, es ist einfach, die 65k Methode Grenze zu treffen.

Wenn Sie dies in Ihrem build.gradle:

compile 'com.google.android.gms:play-services:10.0.1' 

Ersetzen Sie es mit diesem:

compile 'com.google.android.gms:play-services-maps:10.0.1' 

Für weitere Informationen the documentation sehen.

+0

dies funktionierte für mich danke – pavitran