0
import android.app.Activity; 
    import android.os.Bundle; 
    import android.support.v4.app.FragmentActivity; 
import android.view.Menu; 
    import android.view.View; 
    import android.widget.Toast; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GooglePlayServicesUtil; 
import com.google.android.gms.maps.CameraUpdate; 
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.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

public class GymFind extends Activity { 

    // gets stuck on any call to LatLng static final LatLng LOCATION_BRAY = new LatLng(53.183304,-6.123726); 
static final LatLng LOCATION_Greystones = new LatLng(53.136525,-6.065014); 
static final LatLng LOCATION_Charlesland = new LatLng(53.122311,-6.064846); 
private GoogleMap map; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.gmaps); 


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

     map.addMarker(new MarkerOptions().position(LOCATION_BRAY).title("Find us here!")); 
     map.addMarker(new MarkerOptions().position(LOCATION_Greystones).title("Find us here!")); 
     map.addMarker(new MarkerOptions().position(LOCATION_Charlesland).title("Find us here!")); 
} 

расположение:При попытке использовать Google Maps API вызовов я получаю во время выполнения исключение вызвано NullPointer исключением

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

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:text="Hello World" /> 
<fragment 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    class="com.google.android.gms.maps.MapFragment"/> 

</RelativeLayout> 

LogCat:

07-24 13:33:57.423: E/AndroidRuntime(1353): FATAL EXCEPTION: main 
     07-24 13:33:57.423: E/AndroidRuntime(1353): java.lang.RuntimeException: Unable to  start activity 
     java.lang.NullPointerException 
    07-24 13:33:57.423: E/AndroidRuntime(1353): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
    07-24 13:33:57.423: E/AndroidRuntime(1353): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
    07-24 13:33:57.423: E/AndroidRuntime(1353): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
    07-24 13:33:57.423: E/AndroidRuntime(1353): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
    07-24 13:33:57.423: E/AndroidRuntime(1353): at android.os.Handler.dispatchMessage(Handler.java:99) 
    07-24 13:33:57.423: E/AndroidRuntime(1353): at android.os.Looper.loop(Looper.java:137) 
    07-24 13:33:57.423: E/AndroidRuntime(1353): at android.app.ActivityThread.main(ActivityThread.java:5041) 
    07-24 13:33:57.423: E/AndroidRuntime(1353): at java.lang.reflect.Method.invokeNative(Native Method) 
    07-24 13:33:57.423: E/AndroidRuntime(1353): at java.lang.reflect.Method.invoke(Method.java:511) 
    07-24 13:33:57.423: E/AndroidRuntime(1353): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
    07-24 13:33:57.423: E/AndroidRuntime(1353): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
    07-24 13:33:57.423: E/AndroidRuntime(1353): at dalvik.system.NativeStart.main(Native Method) 
    07-24 13:33:57.423: E/AndroidRuntime(1353): Caused by: java.lang.NullPointerException 
    07-24 13:33:57.423: E/AndroidRuntime(1353): at com.thedesignpool.shorelineleisure.GymFind.onCreate(GymFind.java:35) 
    07-24 13:33:57.423: E/AndroidRuntime(1353): at android.app.Activity.performCreate(Activity.java:5104) 
    07-24 13:33:57.423: E/AndroidRuntime(1353): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
    07-24 13:33:57.423: E/AndroidRuntime(1353): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
+0

Посмотрите на 'GymFind.java: 35'. –

+0

Кажется, что 'findFragmentById()' return 'null'. –

ответ

1

Это потому, что вы пытаетесь получить map поле сразу после настройки содержимого, но он будет доступен только после вызова фрагмента onCreateView(), поэтому вам следует сбросить map и работать с ним до onCreateView().

+0

спасибо, что так много парней. Теперь он говорит, что мне нужно обновить мои сервисы Google Play, чтобы я это сделал. Также это не позволяет мне переопределить мою функцию onCreateView, где я должен писать код карты. –

+0

Вы должны расширить 'com.google.android.gms.maps.MapFragment' и переопределить его там. – Desert

0

Использование:

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

вместо этого:

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

Это не позволит мне использовать функцию getSupportFragmentManager. Он говорит, что нельзя отбрасывать фрагмент в mapfragment. так что я должен изменить то, что мой класс расширяет и что я продюсирую? Большое спасибо. –