10

I am investigating the excellent Mapbox library within my current Android Application.

I have come across this issue when trying to integrate the 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)

My applications style is as follows:-

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

The versions I am employing are:-

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

I configure my toolbar as follows:-

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

    }

My activity layout resembles this:-

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

Why cant the Mapbox PlacePickerActivity hide my actionbar/toolbar?

  @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);
  }
Hector
  • 4,016
  • 21
  • 112
  • 211

2 Answers2

0

The problem is that you need to call (AppCompatActivity)getActivity()).getSupportActionBar(), if you're using Toolbar as the ActionBar

In case that doesn't help, the answer to your question can be found in the links below because there are many solutions depend on the different scenario.

See this: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

I_Al-thamary
  • 3,385
  • 2
  • 24
  • 37
  • 1
    the problem is the getSupportActionBar().hide(); code is held within the MapBox library. It appears MapBox assumes the hosting Android application will never employ a Theme without an actionBar – Hector Jan 03 '19 at 09:39
  • I found this it may it be helpful. Dismiss ToolBar for all activities with AppTheme: styles.xml: ` – I_Al-thamary Jan 03 '19 at 10:18
  • @Hector See the first link in the answer. – I_Al-thamary Jan 03 '19 at 13:03
0

@Hector's comment is correct - this was caused by the fact that getSupportActionBar().hide() was called directly within the Mapbox Places plugin.

However, this was addressed in v0.7.0 and should no longer be a problem.

riastrad
  • 1,624
  • 10
  • 22