1

前幾天我問了一個這樣的問題...... this。我將其標記爲已解決,因爲錯誤是isPlayServicesAvailable()返回false,因爲我還沒有最新的Google Play服務。我現在擁有它們......但仍然是NPE。fManager.getFragmentById()返回null

代碼:

GoogleMap map; 
FragmentManager fmanager = getSupportFragmentManager(); 
Fragment fragment = fmanager.findFragmentById(R.id.map);//after this, fragment is null 
SupportMapFragment supportmapfragment = (SupportMapFragment)fragment; 
map=supportmapfragment.getMap();//Got exception here 

在XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<fragment 
    android:id="@+id/map" 
    android:name="com.google.android.gms.maps.SupportMapFragment" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    /> 



</RelativeLayout> 

我得到這個異常:

10-23 13:54:53.230: E/AndroidRuntime(6786): java.lang.RuntimeException: Unable to start activity ComponentInfo{mypackage/mypackage.MapActivity}: java.lang.NullPointerException 
10-23 13:54:53.230: E/AndroidRuntime(6786):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 
10-23 13:54:53.230: E/AndroidRuntime(6786):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
10-23 13:54:53.230: E/AndroidRuntime(6786):  at android.app.ActivityThread.access$600(ActivityThread.java:123) 
10-23 13:54:53.230: E/AndroidRuntime(6786):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
10-23 13:54:53.230: E/AndroidRuntime(6786):  at android.os.Handler.dispatchMessage(Handler.java:99) 
10-23 13:54:53.230: E/AndroidRuntime(6786):  at android.os.Looper.loop(Looper.java:137) 
10-23 13:54:53.230: E/AndroidRuntime(6786):  at android.app.ActivityThread.main(ActivityThread.java:4424) 
10-23 13:54:53.230: E/AndroidRuntime(6786):  at java.lang.reflect.Method.invokeNative(Native Method) 
10-23 13:54:53.230: E/AndroidRuntime(6786):  at java.lang.reflect.Method.invoke(Method.java:511) 
10-23 13:54:53.230: E/AndroidRuntime(6786):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817) 
10-23 13:54:53.230: E/AndroidRuntime(6786):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584) 
10-23 13:54:53.230: E/AndroidRuntime(6786):  at dalvik.system.NativeStart.main(Native Method) 
10-23 13:54:53.230: E/AndroidRuntime(6786): Caused by: java.lang.NullPointerException 
10-23 13:54:53.230: E/AndroidRuntime(6786):  at mypackage.MapActivity.onCreate(MapActivity.java:73) 
10-23 13:54:53.230: E/AndroidRuntime(6786):  at android.app.Activity.performCreate(Activity.java:4470) 
10-23 13:54:53.230: E/AndroidRuntime(6786):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
10-23 13:54:53.230: E/AndroidRuntime(6786):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 

我怎麼能有我的地圖工作...?

+0

轉問... HTTP://stackoverflow.com/questions/15762266/findfragmentbyid-always- returns-a-null –

回答

0

嘗試 -

GoogleMap map; 
FragmentManager fmanager = getSupportFragmentManager(); 
SupportMapFragment supportmapfragment = (SupportMapFragment)fmanager.findFragmentById(R.id.map);; 
map=supportmapfragment.getMap();//Got exception here 

加更改的XML片段聲明

<fragment 
android:id="@+id/map" 
class="com.google.android.gms.maps.SupportMapFragment" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignParentBottom="true" 
android:layout_alignParentLeft="true" 
android:layout_alignParentTop="true" 
/> 
+0

不幸的是...空指針異常... – Fustigador

+0

好吧,我不能幫助你更遠的代碼,如果你想更多的幫助發佈你的代碼... 順便說一句,檢查這篇文章的更多幫助 - http:// dum miesnotes.blogspot.co.il/2013/07/create-android-project-google-map-v2.html – crazyPixel