1

我試圖做到這一點谷歌地圖V2沒有movecamera,它是做工精細,但我嘗試使用movecamera,使問題。 我做了谷歌播放服務,LIB添加到我的項目,我使用IntelliJ IDEA的 乳寧在設備上的Android 4.0.4與谷歌播放服務顯示java.lang.NullPointerException谷歌地圖API 2

logcat的

12-14 00:57:30.978 26218-26218/com.example.maptest2 E/AndroidRuntime﹕ FATAL EXCEPTION: main 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.maptest2/com.example.maptest2.MyActivity}: java.lang.NullPointerException 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992) 
     at android.app.ActivityThread.access$600(ActivityThread.java:127) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158) 
     at android.os.Handler.dispatchMessage(Handler.java:99) 
     at android.os.Looper.loop(Looper.java:137) 
     at android.app.ActivityThread.main(ActivityThread.java:4441) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:511) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565) 
     at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NullPointerException 
     at com.example.maptest2.MyActivity.onCreate(MyActivity.java:37) 
     at android.app.Activity.performCreate(Activity.java:4465) 
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992) 
            at android.app.ActivityThread.access$600(ActivityThread.java:127) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158) 
            at android.os.Handler.dispatchMessage(Handler.java:99) 
            at android.os.Looper.loop(Looper.java:137) 
            at android.app.ActivityThread.main(ActivityThread.java:4441) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:511) 
            atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:74 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565) 
            at dalvik.system.NativeStart.main(Native Method) 

activity_map .XML

 <?xml version="1.0" encoding="utf-8"?> 

    <fragment xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:map="http://schemas.android.com/apk/res-auto" 
        android:id="@+id/map" 
        android:name="com.google.android.gms.maps.MapFragment" 
        android:orientation="vertical" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        map:cameraTargetLat="35.721988" 
        map:cameraTargetLng="51.42128" 
        map:cameraZoom="10" 
      /> 

MyActivity.java

 package com.example.maptest2; 

    import android.UnusedStub; 
    import android.app.Activity; 
    import android.app.Dialog; 
    import android.app.Fragment; 
    import android.app.FragmentTransaction; 
    import android.os.Bundle; 
    import android.view.Menu; 
    import android.widget.Toast; 
    import com.google.android.gms.common.ConnectionResult; 
    import com.google.android.gms.common.GooglePlayServicesUtil; 
    import com.google.android.gms.maps.*; 
    import com.google.android.gms.maps.model.LatLng; 

    public class MyActivity extends Activity { 

     private static final int GPS_ERRORDIALOG_REQUEST = 9001; 
     private GoogleMap map ; 


     @SuppressWarnings("unused") 
     private static final double Eram_park_Lat=35.7192, 
     Eram_park_Lng=51.294422; 

     private static final float DEFAULZOOM =13; 
     /** 
     * Called when the activity is first created. 
     */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      if(servicesOK()){ 
       Toast.makeText(this, "Ready to map!", Toast.LENGTH_SHORT).show(); 
       setContentView(R.layout.activity_maps); 
       LatLng ll = new LatLng(Eram_park_Lat, Eram_park_Lng); 
       map.moveCamera(CameraUpdateFactory.newLatLngZoom(ll,DEFAULZOOM)); 
      } 
      else { 
       setContentView(R.layout.main); 
      } 
     } 

     public boolean servicesOK() { 
      int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 

      if (isAvailable == ConnectionResult.SUCCESS) { 
       return true; 
      } 
      else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) { 
       Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, GPS_ERRORDIALOG_REQUEST); 
       dialog.show(); 
      } 
      else { 
       Toast.makeText(this, "Can't connect to Google Play services", Toast.LENGTH_SHORT).show(); 
      } 
      return false; 
     } 



    } 

AndroidManifest.xml中

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

     <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="18"/> 

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

     <uses-permission android:name="com.example.maptest2.permission.MAPS_RECEIVE" /> 
     <uses-permission android:name="android.permission.INTERNET"/> 
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
     <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> 
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 

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


     <application android:label="@string/app_name" android:icon="@drawable/ic_launcher"> 
      <activity android:name="MyActivity" 
         android:label="@string/app_name"> 
       <intent-filter> 
        <action android:name="android.intent.action.MAIN"/> 
        <category android:name="android.intent.category.LAUNCHER"/> 
       </intent-filter> 
      </activity> 
      <uses-library android:name="com.google.android.maps"/> 
      <meta-data 
        android:name="com.google.android.gms.version" 
        android:value="@integer/google_play_services_version" /> 
      <meta-data 
        android:name="com.google.android.maps.v2.API_KEY" 
        android:value="api key"/> 

     </application> 
    </manifest> 
+1

您的Goog leMap變量映射爲空 – CSmith

+0

我是初學者,如何解決它? –

回答

1

您必須初始化GoogleMap對象,像這樣:

map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 

https://developers.google.com/maps/documentation/android/map嚴格遵循一起,這將讓你去

+0

非常感謝。它的工作,但我嘗試單獨使用getFragmentManager,不接受。我複製所有,這是工作。 –