Android MapFragment getMap()返回null(mapBox)
我有一个map offline mapBox,但由于它为空,所以没有加载该映射。我遵循现场的指示: 布局中Android MapFragment getMap()返回null(mapBox),android,mapbox,mapfragment,Android,Mapbox,Mapfragment,我有一个map offline mapBox,但由于它为空,所以没有加载该映射。我遵循现场的指示: 布局中 <com.mapbox.mapboxsdk.views.MapView android:id="@+id/mapView" android:layout_width="fill_parent" android:layout_height="400dp" android:layout_marginTop="30dp" android:layo
<com.mapbox.mapboxsdk.views.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="400dp"
android:layout_marginTop="30dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
mapbox:access_token="@string/access_token"
/>
错误是:
java.lang.NullPointerException
at com.mapbox.mapboxsdk.MapFragment.onStart(MapFragment.java:70)
为什么要扩展MapFragment并定义自己的布局?无论如何,这看起来好像在
R.layout.fragment\u映射中没有R.id.mapView
。如果没有完整的布局和片段加载逻辑,就不能说更多了
下面是一些有效的代码(使用mapbox SDK 2.2和Android SDK 23):
main活动
class:
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
// Load the fragment dynamically
getFragmentManager()
.beginTransaction()
.add(R.id.fragment_container_layout, MyMapFragment.create())
.commit();
}
}
自定义片段:
import com.mapbox.mapboxsdk.MapFragment;
........
public class ConctactsFragment extends MapFragment {
.........
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getActivity();
mRootView = inflater.inflate(R.layout.fragment_map, container, false);
mMapView =(MapView)mRootView.findViewById(R.id.mapView);
XmlPullParser parser=getResources().getXml(R.xml.attrs);
AttributeSet attrs = Xml.asAttributeSet(parser);
mMapView = new MapView(mContext, attrs);
Log.d(TAG, "The mapView in onCreateView: " + this.getMap());
//The mapView in onCreateView: null
mMapView.setStyleUrl(Style.MAPBOX_STREETS);
mMapView.setCenterCoordinate(new LatLng(40.956645, 14.304816));
mMapView.setZoomLevel(11);
return mRootView;
}
@Override
public void onStart() {
Log.d(TAG, "The mapView in OnStart: " +this.getMap());
// The mapView in OnStart: null
super.onStart();
mMapView.onStart();
}
}
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.views.MapView;
public class MyMapFragment extends Fragment {
private MapView mMapView;
public static MyMapFragment create() {
return new MyMapFragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.my_map_fragment, container, false);
mMapView = (MapView) rootView.findViewById(R.id.mb_map_view);
mMapView.setStyleUrl(Style.MAPBOX_STREETS);
mMapView.setCenterCoordinate(new LatLng(40.956645, 14.304816));
mMapView.setZoomLevel(11);
mMapView.onCreate(savedInstanceState); // If this line is omitted there is a native crash in the MapBox library
return rootView;
}
@Override
public void onStart() {
super.onStart();
mMapView.onStart();
}
@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mMapView.onSaveInstanceState(outState);
}
@Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
public void onStop() {
super.onStop();
mMapView.onStop();
}
@Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
}
活动\u main.xml
(主要活动的布局):
但是在onStart(…)mMapView=null中,最好在onResume()中执行代码>mMapView.onStart()代码>之前称为onStart()。应用程序崩溃在onStart()中getMap()方法是如何定义的?是否已设置?不,未设置,这是MapFragment的标准方法我已在fragment中加载mapbox,但在加载之前,它显示的是黑屏,而不是地图。在加载地图之前,如何避免黑屏传单?
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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">
<FrameLayout
android:id="@+id/fragment_container_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mapbox.mapboxsdk.views.MapView
android:id="@+id/mb_map_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
custom:access_token="@string/mb_token"/>
</FrameLayout>