android-toolbar - 如何在没有操作栏的应用程序中使用 Mapbox PlacePicker 事件?

标签 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);
  }

最佳答案

问题是您需要调用(AppCompatActivity)getActivity()).getSupportActionBar() , 如果您使用 Toolbar 作为 ActionBar

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

见: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

关于android-toolbar - 如何在没有操作栏的应用程序中使用 Mapbox PlacePicker 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53912998/

相关文章:

android - 在协调器布局中隐藏工具栏下方的 View

android - 膨胀类 android.support.v7.widget.Toolbar 时出错

java - 在 Android 中使用 Mapbox SDK 绘制圆

java - 在 MapBox 上添加标记的最佳方法是什么?

安卓 : Error Snack bar not showing correctly with Map View

flutter - 在 Flutter 中实现折叠工具栏

android-删除右侧选项菜单上子菜单项的标题?

android - 如何在滚动android时固定我的工具栏

android - 对于标记选择上的信息窗口,Mapbox android SDK 的 BubbleLayout(已弃用)的替代方法是什么?

android - MapBox Android 的缩放控件