2016-09-11 4 views
2

Ich habe versucht, Map v2 zu verwenden. Ich habe den API-Schlüssel mit sha1-Code. aber jedes Mal, wenn ich zu meiner Aktivität gehe, stürzt meine App ab.kann Google Map v2 nicht zum arbeiten

das ist mein Manifest:

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


    <uses-sdk 
     android:minSdkVersion="11" 
     android:targetSdkVersion="17" /> 

    <permission 
     android:name="com.example.mehdi.permission.MAPS_RECEIVE" 
     android:protectionLevel="signature" /> 

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

    <uses-permission android:name="com.example.mehdi.permission.MAPS_RECEIVE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <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" > 

     <meta-data 
      android:name="com.google.android.gms.v2.API_KEY" 
      android:value="my api key is here"/> 
     <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 


     <activity 
      android:name=".Splash" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 




    </application> 

</manifest> 

dies meine Splash Aktivität:

package com.example.mehdi; 

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

import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.MapFragment; 
import com.google.android.gms.maps.model.BitmapDescriptorFactory; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.Marker; 
import com.google.android.gms.maps.model.MarkerOptions; 

public class Splash extends Activity { 
    static final LatLng HAMBURG = new LatLng(53.558, 9.927); 
    static final LatLng KIEL = new LatLng(53.551, 9.993); 
    private GoogleMap map; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.splash); 
      map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)) 
          .getMap(); 
      Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG) 
          .title("Hamburg")); 
      Marker kiel = map.addMarker(new MarkerOptions() 
          .position(KIEL) 
          .title("Kiel") 
          .snippet("Kiel is cool") 
          .icon(BitmapDescriptorFactory 
              .fromResource(R.drawable.ic_launcher))); 

      // Move the camera instantly to hamburg with a zoom of 15. 
      map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15)); 

      // Zoom in, animating the camera. 
      map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null); 
    } 





} 

dies mein splash.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=".Splash" > 

    <fragment 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     class="com.google.android.gms.maps.MapFragment" /> 

</RelativeLayout> 

ich habe Import Google-Play-services_lib ist zu meinem Arbeitsbereich und stellen Sie es auf Bibliothek und benutzte es in meinem Paket.

das ist mein Logcat nach der App in Emulator:

09-11 03:38:11.501: I/ActivityManager(1256): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.mehdi/.Splash} from pid 1406 
09-11 03:38:11.521: D/dalvikvm(1256): GC_FOR_ALLOC freed 1061K, 24% free 7962K/10396K, paused 1ms, total 12ms 
09-11 03:38:11.521: E/SoundPool(1256): error loading /system/media/audio/ui/Effect_Tick.ogg 
09-11 03:38:11.531: W/AudioService(1256): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg 
09-11 03:38:11.531: E/SoundPool(1256): error loading /system/media/audio/ui/Effect_Tick.ogg 
09-11 03:38:11.531: W/AudioService(1256): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg 
09-11 03:38:11.541: E/SoundPool(1256): error loading /system/media/audio/ui/Effect_Tick.ogg 
09-11 03:38:11.541: W/AudioService(1256): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg 
09-11 03:38:11.551: E/SoundPool(1256): error loading /system/media/audio/ui/Effect_Tick.ogg 
09-11 03:38:11.551: W/AudioService(1256): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg 
09-11 03:38:11.551: E/SoundPool(1256): error loading /system/media/audio/ui/Effect_Tick.ogg 
09-11 03:38:11.551: W/AudioService(1256): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg 
09-11 03:38:11.551: E/SoundPool(1256): error loading /system/media/audio/ui/KeypressStandard.ogg 
09-11 03:38:11.551: W/AudioService(1256): Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg 
09-11 03:38:11.551: E/SoundPool(1256): error loading /system/media/audio/ui/KeypressSpacebar.ogg 
09-11 03:38:11.551: W/AudioService(1256): Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg 
09-11 03:38:11.551: E/SoundPool(1256): error loading /system/media/audio/ui/KeypressDelete.ogg 
09-11 03:38:11.551: W/AudioService(1256): Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg 
09-11 03:38:11.551: E/SoundPool(1256): error loading /system/media/audio/ui/KeypressReturn.ogg 
09-11 03:38:11.551: W/AudioService(1256): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg 
09-11 03:38:11.551: E/SoundPool(1256): error loading /system/media/audio/ui/KeypressInvalid.ogg 
09-11 03:38:11.551: W/AudioService(1256): Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg 
09-11 03:38:11.551: W/AudioService(1256): onLoadSoundEffects(), Error -1 while loading samples 
09-11 03:38:11.861: D/dalvikvm(2586): Not late-enabling CheckJNI (already on) 
09-11 03:38:11.861: I/ActivityManager(1256): Start proc com.example.mehdi for activity com.example.mehdi/.Splash: pid=2586 uid=10059 gids={50059, 3003, 1028, 1015} 
09-11 03:38:11.981: D/AndroidRuntime(2586): Shutting down VM 
09-11 03:38:11.981: W/dalvikvm(2586): threadid=1: thread exiting with uncaught exception (group=0xb2d21b20) 
09-11 03:38:11.981: E/AndroidRuntime(2586): FATAL EXCEPTION: main 
09-11 03:38:11.981: E/AndroidRuntime(2586): Process: com.example.mehdi, PID: 2586 
09-11 03:38:11.981: E/AndroidRuntime(2586): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mehdi/com.example.mehdi.Splash}: java.lang.NullPointerException 
09-11 03:38:11.981: E/AndroidRuntime(2586):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
09-11 03:38:11.981: E/AndroidRuntime(2586):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
09-11 03:38:11.981: E/AndroidRuntime(2586):  at android.app.ActivityThread.access$800(ActivityThread.java:135) 
09-11 03:38:11.981: E/AndroidRuntime(2586):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
09-11 03:38:11.981: E/AndroidRuntime(2586):  at android.os.Handler.dispatchMessage(Handler.java:102) 
09-11 03:38:11.981: E/AndroidRuntime(2586):  at android.os.Looper.loop(Looper.java:136) 
09-11 03:38:11.981: E/AndroidRuntime(2586):  at android.app.ActivityThread.main(ActivityThread.java:5017) 
09-11 03:38:11.981: E/AndroidRuntime(2586):  at java.lang.reflect.Method.invokeNative(Native Method) 
09-11 03:38:11.981: E/AndroidRuntime(2586):  at java.lang.reflect.Method.invoke(Method.java:515) 
09-11 03:38:11.981: E/AndroidRuntime(2586):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
09-11 03:38:11.981: E/AndroidRuntime(2586):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
09-11 03:38:11.981: E/AndroidRuntime(2586):  at dalvik.system.NativeStart.main(Native Method) 
09-11 03:38:11.981: E/AndroidRuntime(2586): Caused by: java.lang.NullPointerException 
09-11 03:38:11.981: E/AndroidRuntime(2586):  at com.example.mehdi.Splash.onCreate(Splash.java:25) 
09-11 03:38:11.981: E/AndroidRuntime(2586):  at android.app.Activity.performCreate(Activity.java:5231) 
09-11 03:38:11.981: E/AndroidRuntime(2586):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
09-11 03:38:11.981: E/AndroidRuntime(2586):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
09-11 03:38:11.981: E/AndroidRuntime(2586):  ... 11 more 
09-11 03:38:12.091: E/BufferQueue(918): [com.android.launcher/com.android.launcher2.Launcher] cancelBuffer: slot 0 is not owned by the client (state=3) 
09-11 03:38:12.511: W/ActivityManager(1256): Force finishing activity com.example.mehdi/.Splash 
09-11 03:38:12.841: I/WindowManager(1256): Screenshot max retries 4 of Token{b3355e18 ActivityRecord{b34e9d78 u0 com.example.mehdi/.Splash t7 f}} appWin=Window{b3374de0 u0 Starting com.example.mehdi} drawState=4 
09-11 03:38:12.841: W/WindowManager(1256): Screenshot failure taking screenshot for (800x1280) to layer 21010 
09-11 03:38:13.021: D/dalvikvm(1375): GC_FOR_ALLOC freed 399K, 26% free 4631K/6176K, paused 46ms, total 46ms 
09-11 03:38:13.081: D/DropBoxEntryAddedChimeraService(1508): User is not opted-in to Usage & Diagnostics or Lockbox. 
09-11 03:38:13.361: W/ActivityManager(1256): Activity pause timeout for ActivityRecord{b34e9d78 u0 com.example.mehdi/.Splash t7 f} 

Ich habe auch versucht App auf mehreren Android physischen Geräten beginnen aber alle von ihnen abgestürzt.

Ich löste das Problem. Danke an @ gaurav4sarma.

Lösung:

i api> 20 für diese Angelegenheit verwendet. niedrigere API war das Problem. Ich musste Support v7 hinzufügen, um AppCompatActivity zu erweitern.

+0

splash.java 25 ?? – kgandroid

+0

Zeile 25: map = ((MapFragment) getFragmentManager(). FindFragmentById (R.id.map)).getMap(); –

Antwort

1

Der Fehler, den Sie erhalten, ist ein NPE-Fehler für das Kartenfragment. Der Hauptgrund ist, dass Sie nicht das richtige Inhaltslayout innerhalb der setContentView-Methode in onCreate festlegen.

setContentView(R.layout.activity_main); //Your Layout is splash.xml 

ändern es zu

setContentView(R.layout.splash); 

Auch wenn dies die NPE Problem lösen wird. Ich würde noch empfehlen die Support-Bibliothek Version Karte verwenden und tun dann Ihre Karte Operationen innerhalb der Asynchron-Rückruf für die Karte wie unten

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

</RelativeLayout> 

Und in Ihrem OnCreate die Karte instanziiert wie folgt

public class Splash extends AppCompatActivity implements OnMapReadyCallback { 

    static final LatLng HAMBURG = new LatLng(53.558, 9.927); 
    static final LatLng KIEL = new LatLng(53.551, 9.993); 
    SupportMapFragment mapFragment; 

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

     mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
    } 

    @Override 
    public void onMapReady(GoogleMap map) { 
     Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG) 
         .title("Hamburg")); 
     Marker kiel = map.addMarker(new MarkerOptions() 
         .position(KIEL) 
         .title("Kiel") 
         .snippet("Kiel is cool") 
         .icon(BitmapDescriptorFactory 
             .fromResource(R.drawable.ic_launcher))); 

     // Move the camera instantly to hamburg with a zoom of 15. 
     map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15)); 

     // Zoom in, animating the camera. 
     map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null); 
    } 
} 
+0

Ich habe Ihren Code ausprobiert, aber es gab Probleme mit getSupportFragmentManager und getMapAsync und onMapReady. –

+0

Könnten Sie bitte Ihren Stacktrace Logcat für das Problem freigeben? –

+0

1- Verfahren getSupportFragmentManager() ist für die Art Splash 2- Verfahren getMapAsync (OnMapReadyCallback) in der Art SupportMapFragment undefiniert ist nicht anwendbar für die Argumente (Splash) 3 - ungültig ist ein ungültiger Typ für die variable onMapReady Ich denke mein Klassentyp ist falsch, was sollte die Klasse ausdehnen? jetzt erweitert es Aktivität –

0

ersetzen Sie erstreckt Aktivität mit FragmentActivity

public class MainActivity extends FragmentActivity { 

auch die Karte auf diese Weise verwenden:

SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager() 
        .findFragmentById(R.id.map); 
    map = mapFragment .getMap(); 

Ihre xml ändern:

<fragment 
     android:id="@+id/map" 
     android:name="com.google.android.gms.maps.MapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" /> 

Das android:name Attribut ist important.Without es null zurück abbildet.

Hinweis: Wenn android:name="com.google.android.gms.maps.MapFragment" nicht work.then versuchen android:name="com.google.android.gms.maps.SupportMapFragment"

+0

Immer noch nicht funktioniert –

+0

sehe meine bearbeitete Antwort – kgandroid

+0

Nein noch Absturz passiert –

Verwandte Themen