2016-09-11 6 views
2

Map v2를 사용해 보았습니다. sha1 코드를 사용하여 api 키를 얻었습니다. 하지만 매번 내 활동에 갈 때마다 앱이 다운됩니다. 이 내 splash.xmlGoogle지도 v2를 만들 수 없습니다

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

내가 한 수입 구글 플레이-services_lib입니다

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); 
    } 





} 

이 내 스플래쉬 활동이

<?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> 

입니다 :

내 매니페스트입니다 내 작업 공간으로 가져 와서 라이브러리로 설정하고 내 패키지에 사용했습니다.

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} 

내가 여러 안드로이드 물리적 장치에 응용 프로그램을 시작하려했지만 그들 모두가 추락 :

이 에뮬레이터에서 응용 프로그램을 실행 한 후 내 로그 캣입니다.

이 문제가 해결되었습니다. @ gaurav4sarma 덕택입니다.

용액 :이 문제에 대한 API> 20을 이용

. 낮은 API가 문제였습니다. AppCompatActivity를 확장하려면 지원 v7을 추가해야했습니다.

+0

splash.java (25)를 시도 work.then하지? – kgandroid

+0

line 25 : map = ((MapFragment) getFragmentManager(). findFragmentById (R.id.map)).getMap(); –

답변

1

오류는 맵 조각에 대한 NPE 오류입니다. 주된 이유는 onCreate 내의 setContentView 메소드 내에 올바른 컨텐츠 레이아웃을 설정하지 않기 때문입니다. 이것은 NPE 문제를 해결할 수 있지만

setContentView(R.layout.splash); 

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

변경을.

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

나는 당신의 코드를 시도했지만 getSupportFragmentManager 및 getMapAsync 및 onMapReady에 문제가있었습니다. –

+0

문제에 대해 stacktrace logcat을 알려주십시오. –

+0

1- 방법에서 getSupportFragmentManager()는 입력 SupportMapFragment의 형 플래쉬 2-있어서 getMapAsync (OnMapReadyCallback)에 대한 정의되는 인자 (스플래시) 3 적용되지 - 공극이 onMapReady 변수에 대한 잘못된 유형 내 수업 유형이 잘못되었다고 생각합니다. 수업을 어떻게 확장해야합니까? 이제 액티비티가 확장됩니다. –

0

을 다음과 같이 나는 아직도지도의 지원 라이브러리 버전을 사용하는 것이 좋습니다 다음에서 OnCreate지도를 인스턴스화

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

아래 내부처럼지도의 비동기 콜백 내에서지도 작업을해야합니까

: FragmentActivity 또한

public class MainActivity extends FragmentActivity { 

이 방법으로지도를 사용하여 확장 활동을 교체

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

android:name 속성 important.Without는 null를 돌려지도입니다 :

SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager() 
        .findFragmentById(R.id.map); 
    map = mapFragment .getMap(); 
은 당신의 XML을 변경

.

참고 : android:name="com.google.android.gms.maps.MapFragment"가하는 경우 android:name="com.google.android.gms.maps.SupportMapFragment"

+0

아직도 작동하지 않습니다 –

+0

내 편집 된 답변보기 – kgandroid

+0

아직 충돌이 발생하지 않습니다. –

관련 문제