2016-08-29 153 views
0

我想在Fragment類的標記內顯示Google地圖中的當前位置。地圖已經顯示,但我得到一個NullpointerException,當我初始化MapFragment顯示Google地圖中當前位置的標記

MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map); 
mapFragment.getMapAsync(this); 

這是onMapReady()功能:

@Override 
public void onMapReady(GoogleMap googleMap) { 

    this.googlemap_global = googleMap; 
    googlemap_global.setMyLocationEnabled(true); 
    googlemap_global.setTrafficEnabled(true); 
    googlemap_global.setIndoorEnabled(true); 
    googlemap_global.setBuildingsEnabled(true); 
    googlemap_global.getUiSettings().setZoomControlsEnabled(true); 

} 

而這正是佈局文件:

<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" 
android:orientation="horizontal" 
tools:context=".GPSTracker2" > 


<fragment 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_height="400dp" 
    class="com.google.android.gms.maps.MapFragment" /> 

<Button 
    android:id="@+id/gps_service_start_stop_button" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="GPS Service starten"/> 

這裏

我這個初始化試了一下我設置了標記:

LatLng latlng = new LatLng(latitude_global,longitude_global); 

    if(googlemap_global != null){ 
     Marker aktueller_standort = googlemap_global.addMarker(new MarkerOptions().position(latlng).title("Standort")); 

     googlemap_global.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, 15)); 

     // Zoom in, animating the camera. 
     googlemap_global.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null); 
    } 

這裏是logcat的輸出:

java.lang.NullPointerException: Attempt to invoke virtual method 

'android.app.Fragment android.app.FragmentManager.findFragmentById(int)' on a null object reference 
at com.gpstracker.viktor.gpstracker2.Home_Fragment.<init>(Home_Fragment.java:82) 
at com.gpstracker.viktor.gpstracker2.GPSTracker2.selectItemFromDrawer(GPSTracker2.java:105) 
at com.gpstracker.viktor.gpstracker2.GPSTracker2.access$000(GPSTracker2.java:28) 
at com.gpstracker.viktor.gpstracker2.GPSTracker2$1.onItemClick(GPSTracker2.java:67) 
at android.widget.AdapterView.performItemClick(AdapterView.java:310) 
at android.widget.AbsListView.performItemClick(AbsListView.java:1145) 
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3066) 
at android.widget.AbsListView$3.run(AbsListView.java:3903) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5461) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

感謝您的幫助。

+0

你能告訴你的XML文件 – Soham

回答

2

呦烏爾XML文件

<FrameLayout 
     android:id="@+id/mapContainer" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

的Java文件代碼在地圖上

FrameLayout mapContainer = (FrameLayout) findViewById(R.id.mapContainer); 
//show map of 40% of screen 
DisplayMetrics dm = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(dm); 
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
      ViewGroup.LayoutParams.MATCH_PARENT); 
    params.height = (int) ((float) dm.heightPixels * (float) 0.5); 
    mapContainer.setLayoutParams(params); 
    FragmentManager fm = getFragmentManager(); 
    MapFragment mapFragment = MapFragment.newInstance(); 
    fm.beginTransaction().replace(R.id.mapContainer, mapFragment).commit(); 

mapFragment.getMapAsync(new OnMapReadyCallback() { 
       @Override 
       public void onMapReady(GoogleMap googleMap) { 
        GoogleMap mMap = googleMap; 
        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
        addMarks(mMap); 

顯示標記

private void addMarks(GoogleMap googleMap) { 
    LatLngBounds.Builder builder = new LatLngBounds.Builder(); 
    LatLng latLng = new LatLng(lat1, long1); 
    builder.include(latLng); 
    MarkerOptions markerOptions = new MarkerOptions(); 
    markerOptions.position(latLng); 
    markerOptions.title(currentLocation); 
    googleMap.addMarker(markerOptions); 

    } 
1

更改這個像下面

<fragment 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_height="400dp" 
    class="com.google.android.gms.maps.MapFragment" /> 

<fragment 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_height="400dp" 
     class="com.google.android.gms.maps.SupportMapFragment" /> 

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

SupportMapFragment branchdetails_map = 
       (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
相關問題