【发布时间】: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);
}
【问题讨论】:
-
尝试删除 requestWindowFeature(Window.FEATURE_NO_TITLE);
-
@GovindParasha,失败的代码不是我的。它的 MapBox 库假定托管 Activity 主题始终使用 actionBar
-
AppCompat-v7 支持库已添加到您的应用程序中?
-
@Hector 最后提供的链接解决了您的问题吗?
标签: android-toolbar mapbox-android