gpt4 book ai didi

java - 在 MapView 中使用 Bottomsheet 时出现错误

转载 作者:行者123 更新时间:2023-12-02 08:57:43 24 4
gpt4 key购买 nike

我创建了一个 MapView,其中使用从数据库调用的经度和纬度来绘制多个标记。

现在,我想在单击此标记时显示 Bottom Sheet 并在工作表上显示特定数据。我从这里获取了一些 Bottom Sheet 的引用Slide in view from bottom over Google Map on Marker click

但是在执行这篇文章后我收到一个错误

错误是

E/AndroidRuntime: FATAL EXCEPTION: main
Process: part.time.job.v2, PID: 4236
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.material.bottomsheet.BottomSheetBehavior.setPeekHeight(int)' on a null object reference
at part.time.job.v2.LabourFragment.onCreateView(LabourFragment.java:115)

这是我的JAVA代码

package part.time.job.v2;


import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.android.libraries.places.api.Places;
import com.google.android.libraries.places.api.model.Place;
import com.google.android.libraries.places.api.model.PlaceLikelihood;
import com.google.android.libraries.places.api.net.FindCurrentPlaceRequest;
import com.google.android.libraries.places.api.net.FindCurrentPlaceResponse;
import com.google.android.libraries.places.api.net.PlacesClient;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import com.google.maps.android.ui.IconGenerator;

import java.util.Arrays;
import java.util.List;

import model.Jobpost;

import static com.android.volley.VolleyLog.TAG;


/**
* A simple {@link Fragment} subclass.
*/
public class LabourFragment extends Fragment implements OnMapReadyCallback {
private GoogleMap mGoogleMap;
private MapView mMapview;
private int STORAGE_PERMISSION_CODE = 1;
private CameraPosition mCameraPosition;
private FusedLocationProviderClient mFusedLocationProviderClient;

private final LatLng mDefaultLocation = new LatLng(-33.8523341, 151.2106085);
private static final int DEFAULT_ZOOM = 15;
private static final int PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;
private boolean mLocationPermissionGranted;

private Location mLastKnownLocation;
Dialog myDialog;
private BottomSheetBehavior bottomSheetBehavior;
private View bottomSheet;


// Keys for storing activity state.
private static final String KEY_CAMERA_POSITION = "camera_position";
private static final String KEY_LOCATION = "location";

// Used for selecting the current place.
private static final int M_MAX_ENTRIES = 5;
private String[] mLikelyPlaceNames;
private String[] mLikelyPlaceAddresses;
private List[] mLikelyPlaceAttributions;
private LatLng[] mLikelyPlaceLatLngs;
private PlacesClient mPlacesClient;

public LabourFragment() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {



if (savedInstanceState != null) {
mLastKnownLocation = savedInstanceState.getParcelable(KEY_LOCATION);
mCameraPosition = savedInstanceState.getParcelable(KEY_CAMERA_POSITION);
}
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_labour, container, false);
// myDialog = new Dialog(getActivity());
bottomSheet = view.findViewById(R.id.bottom_sheet);
bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet); //error occur on this line
bottomSheetBehavior.setPeekHeight(200);
bottomSheetBehavior.setHideable(true);
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);


Places.initialize(getActivity(), getString(R.string.google_maps_key));
mPlacesClient = Places.createClient(getActivity());


mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getActivity());


return view;
}

@Override
public void onSaveInstanceState(Bundle outState) {
if (mGoogleMap != null) {
outState.putParcelable(KEY_CAMERA_POSITION, mGoogleMap.getCameraPosition());
outState.putParcelable(KEY_LOCATION, mLastKnownLocation);
super.onSaveInstanceState(outState);
}
}

@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mMapview = (MapView) view.findViewById(R.id.mapView);

if (mMapview!=null){
mMapview.onCreate(null);
mMapview.onResume();
mMapview.getMapAsync(this);
}
}



@Override
public void onMapReady(final GoogleMap googleMap) {

mGoogleMap=googleMap;

FirebaseFirestore mDatabase = FirebaseFirestore.getInstance();
CollectionReference mOrderRef = mDatabase.collection("Job Post1");

mOrderRef.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for(QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
if(documentSnapshot.contains("lat") && documentSnapshot.contains("lon")) {
String lat = (String) documentSnapshot.get("lat");
String lon = (String) documentSnapshot.get("lon");
final String title = (String) documentSnapshot.get("title");
final String jobdate = (String) documentSnapshot.get("jobdate");
final String time = (String) documentSnapshot.get("time");



if(lat != null && lon != null && !lat.isEmpty() && !lon.isEmpty()) {
double latitude = Double.parseDouble(lat.trim());
double longitude = Double.parseDouble(lon.trim());
IconGenerator iconGen = new IconGenerator(getActivity());

googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).icon(BitmapDescriptorFactory.fromBitmap(iconGen.makeIcon(title))));
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 18.0f));



googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
updateBottomSheetContent(marker);
return false;
}
});
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
}
});
}

}
}

}

});



}

private void updateBottomSheetContent(Marker marker) {
TextView name = (TextView) bottomSheet.findViewById(R.id.detail_name);
name.setText(marker.getTitle());
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}


}

xml View

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="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="part.time.job.v2.LabourFragment">


<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="@color/purple"
app:itemIconTint="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/nav_items"
tools:ignore="MissingConstraints" />

<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="675dp"
tools:ignore="MissingConstraints" />

<include layout="@layout/bottom_sheet" />



</androidx.constraintlayout.widget.ConstraintLayout>

bottom_sheet xml

  <?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true">


<androidx.core.widget.NestedScrollView android:id="@+id/bottom_sheet"
android:layout_width="match_parent" android:layout_height="550dp"
android:background="@android:color/white" android:clipToPadding="true"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">


<TextView android:id="@+id/detail_name" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_margin="25dp"
android:layout_weight="3" android:gravity="center_vertical"
android:textAppearance="?android:textAppearanceLarge" />

</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

最佳答案

我已经检查了您的代码,它按预期显示了 Bottom Sheet 。但也存在一些问题。

  • Bottom Sheet 的大部分部分隐藏在 BottomNavigationView 后面。为了克服这个问题,请稍微增加 PeekHeight
bottomSheetBehavior.setPeekHeight(300);
  • 您将 Bottom Sheet 容器的 BackgroundColor 设置为 android:color/white。尝试将其更改为不同的颜色。
android:background="@android:color/holo_green_light"
  • 您将 detail_namelayout_width 设置为 0dp,这会使 View 不可见。尝试使其match_parent
android:layout_width="match_parent"

这是底板容器的完整布局:

<androidx.core.widget.NestedScrollView
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="550dp"
android:background="@android:color/holo_green_light"
android:clipToPadding="true"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">


<TextView
android:id="@+id/detail_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="25dp"
android:layout_weight="3"
android:gravity="center_vertical"
android:textAppearance="?android:textAppearanceLarge" />

</androidx.core.widget.NestedScrollView>

输出:
enter image description here

关于java - 在 MapView 中使用 Bottomsheet 时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60388331/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com