3

我想在一個片段中顯示mapbox但我收到以下錯誤工作:Mapbox不片段

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.mapbox.mapboxsdk.maps.MapView.onResume()' on a null object reference

這裏是地圖片段類:

public class Map extends android.support.v4.app.Fragment { 
    private MapView mapView; 

public Map() { 
    // Required empty public constructor 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View layout = inflater.inflate(R.layout.fragment_map, container, false); 

    MapView mapView = (MapView) layout.findViewById(R.id.mapview); 
    mapView.onCreate(savedInstanceState); 
    mapView.getMapAsync(new OnMapReadyCallback() { 
     @Override 
     public void onMapReady(MapboxMap mapboxMap) { 

      // Set map style 
      mapboxMap.setStyleUrl(Style.MAPBOX_STREETS); 

      // Set the camera's starting position 
      CameraPosition cameraPosition = new CameraPosition.Builder() 
        .target(new LatLng(41.885, -87.679)) // set the camera's center position 
        .zoom(12) // set the camera's zoom level 
        .tilt(20) // set the camera's tilt 
        .build(); 

      // Move the camera to that position 
      mapboxMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 

     } 


    }); 
    return layout; 
} 

@Override 
public void onResume() { 
    super.onResume(); 
    mapView.onResume(); 
} 

@Override 
public void onPause() { 
    super.onPause(); 
    mapView.onPause(); 
} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    mapView.onSaveInstanceState(outState); 
} 

@Override 
public void onLowMemory() { 
    super.onLowMemory(); 
    mapView.onLowMemory(); 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    mapView.onDestroy(); 
} 
} 

上午我在這裏錯過了什麼?我正在關注此鏈接:https://www.mapbox.com/help/first-steps-android-sdk/

+0

請參閱此鏈接http://stackoverflow.com/questions/32601219/trying-放置在一個片段的活動/ 32601861#32601861 – Emil

+0

我使用Mapbox,它仍然是一樣的方式? @Boss –

+0

那麼..我發現你的問題 – Emil

回答

4

您有兩個mapview實例。一個是oncreate方法中的局部變量,另一個是全局變量。

和全局變量不initalized,所以改變這一行

MapView mapView = (MapView) layout.findViewById(R.id.mapview); 

這個

mapView = (MapView) layout.findViewById(R.id.mapview); 

我想你明白這個問題。

-1

請在使用前的任何位置可能是空檢查MapView的或不初始化的時間:

@Override 
public void onResume() { 
    super.onResume(); 
    if(mapView){ 
     mapView.onResume(); 
    } 
} 

@Override 
public void onPause() { 
    super.onPause(); 
    if(mapView){ 
    mapView.onPause(); 
    } 
} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
if(mapView){ 
    mapView.onSaveInstanceState(outState); 
} 
} 

@Override 
public void onLowMemory() { 
    super.onLowMemory(); 
if(mapView){ 
    mapView.onLowMemory(); 
} 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
if(mapView){ 
    mapView.onDestroy(); 
} 
} 

還可以使用一流水平/全球MapView類對象的引用,而不是本地/內新的聲明:

MapView mapView = (MapView) layout.findViewById(R.id.mapview); 

用此替代

mapView = (MapView) layout.findViewById(R.id.mapview);