如何在没有操作栏的应用程序中使用Mapbox PlacePicker活动?

时间:2018-12-24 11:43:30

标签: android-toolbar mapbox-android

我正在研究当前Android应用程序中出色的Mapbox库。

在尝试集成PlacePickerActivity时遇到了这个问题。

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.hide()' on a null object reference
        at com.mapbox.mapboxsdk.plugins.places.picker.ui.PlacePickerActivity.onCreate(PlacePickerActivity.java:65)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)

我的应用程序样式如下:-

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

我使用的版本是:-

// MAPBOX
implementation "com.mapbox.mapboxsdk:mapbox-android-sdk:$mapBoxSdkVersion"
implementation "com.mapbox.mapboxsdk:mapbox-android-plugin-traffic:$mapBoxTrafficVersion"
implementation "com.mapbox.mapboxsdk:mapbox-android-plugin-places:$mapBoxPlacesVersion"

    mapBoxSdkVersion = "6.8.1"
    mapBoxTrafficVersion = "0.6.0"
    mapBoxPlacesVersion = "0.6.0"

我的工具栏配置如下:-

   /**
     * @param toolbar
     */
    private void manageToolbar(final Toolbar toolbar) {
        mToolbar = toolbar;
        setSupportActionBar(mToolbar);

    }

我的活动布局类似于:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:mapbox="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".main.Main">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:layout_alignParentTop="true"
        app:layout_scrollFlags="scroll|enterAlways">

        <android.support.v7.widget.Toolbar
            android:id="@+id/crr_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            android:minHeight="?android:attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:layout_collapseMode="parallax"
            app:layout_scrollFlags="scroll|enterAlways">

            <ProgressBar
                android:id="@+id/toolbar_progress"
                android:layout_width="?android:attr/actionBarSize"
                android:layout_height="?android:attr/actionBarSize"
                android:layout_gravity="end"
                android:indeterminate="true"
                android:indeterminateTint="@android:color/white"
                android:indeterminateTintMode="src_in"
                android:padding="5dp"
                android:rotation="0.9" />

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>

    <com.mapbox.mapboxsdk.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        mapbox:mapbox_cameraTargetLat="40.73581"
        mapbox:mapbox_cameraTargetLng="-73.99155"
        mapbox:mapbox_cameraZoom="11"
        mapbox:mapbox_styleUrl="@string/mapbox_style_mapbox_streets" />

</RelativeLayout>

为什么Mapbox PlacePickerActivity无法隐藏我的操作栏/工具栏?

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Hide any toolbar an apps theme might automatically place in activities. Typically creating an
    // activity style would cover this issue but this seems to prevent us from getting the users
    // application colorPrimary color.
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    **getSupportActionBar().hide(); //!!! NULL POINTER HERE**
    setContentView(R.layout.mapbox_activity_place_picker);

    if (savedInstanceState == null) {
      accessToken = getIntent().getStringExtra(PlaceConstants.ACCESS_TOKEN);
      options = getIntent().getParcelableExtra(PlaceConstants.PLACE_OPTIONS);
    }

    // Initialize the view model.
    viewModel = ViewModelProviders.of(this).get(PlacePickerViewModel.class);
    viewModel.getResults().observe(this, this);

    bindViews();
    addBackButtonListener();
    addChosenLocationButton();
    customizeViews();

    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(this);
  }

2 个答案:

答案 0 :(得分:0)

问题是,如果您将工具栏用作ActionBar,则需要调用(AppCompatActivity)getActivity()).getSupportActionBar()

如果没有帮助,则可以在下面的链接中找到问题的答案,因为有许多解决方案取决于不同的情况。

查看此内容:Android Error [Attempt to invoke virtual method 'void android.app.ActionBar' on a null object reference]

How To Show and hide ActionBar with AppCompat v.7

How To Show and hide ActionBar with AppCompat v.7

答案 1 :(得分:0)

@Hector's comment是正确的-这是由于getSupportActionBar().hide()是直接在Mapbox Places插件中调用的事实引起的。

但是,这个was addressed in v0.7.0不再是问题。