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

레이아웃 :내가 런타임 예외 nullpointer 예외에 의한 취득 ​​구글지도 API 호출을 사용하려고 할 때

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

로그 캣 : 당신이 map 필드를 얻으려고 노력하고 있기 때문에

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

봐 : 35'. –

+0

'findFragmentById()'는'null'을 반환합니다. –

답변

1

입니다 컨텐트 뷰를 설정 한 직후, 프래그먼트의 onCreateView() 호출 후에 만 ​​사용할 수 있으므로 map을 얻고 0까지 작동하도록 설정해야합니다.

+0

너무 많은 guys.so 지금은 내가 그렇게 할거야 내 Google 플레이 서비스를 업데이 트해야합니다. 또한 내 onCreateView 함수를 재정의 할 수 없으므로지도 코드를 작성해야합니다. –

+0

'com.google.android.gms.maps.MapFragment'를 확장하고 거기에서 오버라이드해야합니다. – Desert

0

사용 : 대신의

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

: GymFind.java`에서

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

getSupportFragmentManager 함수를 사용할 수 없습니다. 조각에서 맵 조각으로 변환 할 수 없다고합니다. 그렇다면 수업 시간을 변경하고 수업 시간을 연장해야합니까? 고맙습니다. –