【问题标题】:How to use Mapbox PlacePicker activity in Application with no Actionbar?如何在没有操作栏的应用程序中使用 Mapbox PlacePicker 活动?
【发布时间】:2019-05-23 14:11:38
【问题描述】:

我正在研究当前 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);
  }

【问题讨论】:

标签: android-toolbar mapbox-android


【解决方案1】:

问题是你需要调用(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

【讨论】:

  • 问题出在 getSupportActionBar().hide();代码保存在 MapBox 库中。看来 MapBox 假定托管 Android 应用程序永远不会使用没有 actionBar 的主题
  • 我发现这可能会有所帮助。使用 AppTheme 关闭所有活动的工具栏:styles.xml:&lt;style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"&gt;&lt;!-- .NoActionBar --&gt;
  • @Hector 查看答案中的第一个链接。
【解决方案2】:

@Hector's comment 是正确的 - 这是因为 getSupportActionBar().hide() 是在 Mapbox Places 插件中直接调用的。

不过,这个was addressed in v0.7.0 应该不再是问题了。

【讨论】:

    猜你喜欢
    • 2013-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多