-1

我是新來的android編程。我正在開發一個項目,我必須從Google地圖獲取地圖。未找到eclipse警告或錯誤代碼。我不知道我的代碼出了什麼問題。請幫我google map api v2 android-java.lang.NullPointerException

06-07 09:39:29.357: E/AndroidRuntime(432): FATAL EXCEPTION: main 
06-07 09:39:29.357: E/AndroidRuntime(432): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kulinerku/com.kulinerku.MainActivity}: java.lang.NullPointerException 
06-07 09:39:29.357: E/AndroidRuntime(432): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736) 
06-07 09:39:29.357: E/AndroidRuntime(432): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752) 
06-07 09:39:29.357: E/AndroidRuntime(432): at android.app.ActivityThread.access$1500(ActivityThread.java:123) 
06-07 09:39:29.357: E/AndroidRuntime(432): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993) 
06-07 09:39:29.357: E/AndroidRuntime(432): at android.os.Handler.dispatchMessage(Handler.java:99) 
06-07 09:39:29.357: E/AndroidRuntime(432): at android.os.Looper.loop(Looper.java:126) 
06-07 09:39:29.357: E/AndroidRuntime(432): at android.app.ActivityThread.main(ActivityThread.java:3997) 
06-07 09:39:29.357: E/AndroidRuntime(432): at java.lang.reflect.Method.invokeNative(Native Method) 
06-07 09:39:29.357: E/AndroidRuntime(432): at java.lang.reflect.Method.invoke(Method.java:491) 
06-07 09:39:29.357: E/AndroidRuntime(432): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 
06-07 09:39:29.357: E/AndroidRuntime(432): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 
06-07 09:39:29.357: E/AndroidRuntime(432): at dalvik.system.NativeStart.main(Native Method) 
06-07 09:39:29.357: E/AndroidRuntime(432): Caused by: java.lang.NullPointerException 
06-07 09:39:29.357: E/AndroidRuntime(432): at com.kulinerku.MainActivity.onCreate(MainActivity.java:33) 
06-07 09:39:29.357: E/AndroidRuntime(432): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048) 
06-07 09:39:29.357: E/AndroidRuntime(432): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700) 
06-07 09:39:29.357: E/AndroidRuntime(432): ... 11 more 

我MainActivity.java

package com.kulinerku; 

import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 
import android.view.Menu; 

public class MainActivity extends FragmentActivity{ 
    final int RQS_GooglePlayService = 1; 
    private GoogleMap myMap; 

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

     FragmentManager myFragmentManager = getSupportFragmentManager(); 
     SupportMapFragment mySupportMapFragment = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map); 
     myMap = mySupportMapFragment.getMap(); 

     LatLng jogja = new LatLng(-7.782939, 110.367050); 
     MarkerOptions markerJogja = new MarkerOptions(); 
     markerJogja.position(jogja); 
     markerJogja.title("Tugu Jogja"); 
     markerJogja.snippet("Tugu Jogja adalah landmark Kota Jogja"); 

     myMap.setMapType(GoogleMap.MAP_TYPE_NONE); 
     myMap.addMarker(markerJogja); 
     myMap.getUiSettings().setCompassEnabled(true); 
     myMap.getUiSettings().setZoomControlsEnabled(true); 
     myMap.getUiSettings().setZoomGesturesEnabled(true); 

     myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(jogja, 15)); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu){ 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 
} 

AndroidManifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.kulinerku" 
    android:versionCode="1" 
    android:versionName="1.0" > 

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

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



    <uses-permission android:name="com.kulinerku.permission.MAPS_RECEIVE"/> 
    <uses-permission android:name="android.permission.INTERNET"/> 
    <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:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <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="AIzaSyAEUM5FPpkLC0kPGMOnmyTSEwEcRNLjo7s"/> 

     <activity 
      android:name="com.kulinerku.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> 

    </application> 

</manifest> 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<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" 
      android:name="com.kulinerku.MainActivity"/> 


</RelativeLayout> 
+1

發佈完整的堆棧跟蹤 – Raghunandan

+0

我敢打賭,你的'myMap'對象一片空白 – tyczj

回答

0

一個GoogleMap的可僅有B e在底層地圖系統加載並且片段中的基礎視圖存在時,使用getMap()獲取。這個類自動初始化地圖系統和視圖;但無法保證它準備就緒,因爲這取決於Google Play服務APK的可用性。如果GoogleMap不可用,則getMap()將返回null。

http://developer.android.com/reference/com/google/android/gms/maps/SupportMapFragment.html

請務必遵循使用谷歌播放服務(包括導入項目等),所有的步驟在這裏:http://developer.android.com/google/play-services/setup.html

相關問題