android - MapFragment getMap() 返回 null (mapBox)

标签 android mapbox mapfragment

我有一个离线 map mapBox,但 map 无法加载,因为它为空。我按照网站的指示进行操作:https://www.mapbox.com/android-sdk/

布局中

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

在 fragment 中:

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();
 }


}

错误是:

 java.lang.NullPointerException
 at com.mapbox.mapboxsdk.MapFragment.onStart(MapFragment.java:70)

最佳答案

为什么要扩展 MapFragment定义自己的布局?无论如何,这看起来像您的 R.layout.fragment_map 中没有 R.id.mapView 。如果没有完整的布局和 fragment 加载逻辑,真的不能说更多。

这里是一些有效的代码(使用mapbox SDK 2.2,Android SDK 23):

MainActivity 类:

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();
    }

}

自定义 fragment :

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();
    }
}

activity_main.xml(主要 Activity 的布局):

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

my_map_fragment.xml(自定义 map fragment 的布局):

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

关于android - MapFragment getMap() 返回 null (mapBox),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33752580/

相关文章:

android - 谷歌地图上的比例标记大小

android - 创建数据库和默认数据

ios - MapBox - 检测 zoomLevel 变化

android - 二进制 XML 文件行 #8 : Error inflating class fragment, Google map

Swift Mapbox 自定义用户位置注释( fatal error )

swift - 多个挤压多边形 Mapbox iOS

android - 二进制 XML 文件行 #1 : Error inflating class fragment MapFragment

android - 通过 NFC 标签上的 URL 启动 Android 应用程序

c# - Xamarin,Grpc,无法使用 token 0100002b 解析类型

Android:如何在 ImageView 中显示选定的图片库?