将MapActivity转为MapFragment后出现bottomAppBar视图找不到错误求助
问题描述
最近把MapActivity迁移成MapFragment,其他问题都修复了,但碰到这个错误:
No view found for id 0x7f080060 (com.example.policetracker:id/bottomAppBar) for fragment MapFragment{19507fe} (5f780415-46e3-4362-a236-d9b35c95d427 id=0x7f080060)
刚接触Android Studio和XML,附上MapFragment代码和fragment_map布局代码,求解决方法。
MapFragment代码
public class MapFragment extends Fragment implements OnMapReadyCallback, ActivityCompat.OnRequestPermissionsResultCallback { private GoogleMap mMap; private View mView; Location currentLocation; private FusedLocationProviderClient fusedClient; private static final int REQUEST_CODE = 101; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { mView = inflater.inflate(R.layout.fragment_map, container, false); return mView; } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); fusedClient = LocationServices.getFusedLocationProviderClient(requireActivity()); getLocation(); } private void getLocation(){ if((ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED) && (ActivityCompat.checkSelfPermission( getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)){ ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE); return; } Task<Location> task = fusedClient.getLastLocation(); task.addOnSuccessListener(new OnSuccessListener<Location>() { @Override public void onSuccess(Location location) { if(location != null){ SupportMapFragment supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map); assert supportMapFragment != null; supportMapFragment.getMapAsync(MapFragment.this); } } }); } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; LatLng latLng = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()); mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng)); mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17)); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == REQUEST_CODE) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { getLocation(); } } } }
fragment_map布局代码
<?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" tools:context=".MapFragment"> <androidx.fragment.app.FragmentContainerView android:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent" tools:layout="@android:layout/activity_list_item" /> <com.google.android.material.bottomappbar.BottomAppBar android:id="@+id/bottomAppBar" android:layout_width="match_parent" android:layout_height="wrap_content" app:fabCradleMargin="20dp" app:fabCradleVerticalOffset="10dp" app:fabCradleRoundedCornerRadius="20dp" android:layout_gravity="bottom"> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottomNavigationView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginEnd="16dp" android:background="@android:color/transparent" app:menu="@menu/bottom_nav_menu" /> </com.google.android.material.bottomappbar.BottomAppBar> <com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/baseline_add_24" app:layout_anchor="@id/bottomAppBar" android:contentDescription="TODO" /> </androidx.coordinatorlayout.widget.CoordinatorLayout>
解决方法
这个错误的核心原因是你在添加MapFragment时,错误地把它的容器ID指定成了bottomAppBar的ID,而不是布局中专门用来承载Fragment的容器(比如Activity布局里的FrameLayout或FragmentContainerView)。
步骤1:检查并修正宿主Activity的布局
打开承载MapFragment的Activity布局文件(比如activity_main.xml),确保里面有一个专门的Fragment容器,示例如下:
<!-- 示例Activity布局 --> <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 这个是承载MapFragment的容器,ID要和添加Fragment时用的一致 --> <androidx.fragment.app.FragmentContainerView android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent"/> </androidx.coordinatorlayout.widget.CoordinatorLayout>
步骤2:正确添加MapFragment到容器
在Activity的代码中,添加MapFragment时指定正确的容器ID:
// 在Activity的onCreate方法中 getSupportFragmentManager() .beginTransaction() .replace(R.id.fragment_container, new MapFragment()) // 这里用fragment_container的ID,不是bottomAppBar .commit();
额外修复:MapFragment中的空指针问题
你的onMapReady方法直接使用未赋值的currentLocation会导致空指针异常,修改getLocation方法里的成功回调:
task.addOnSuccessListener(new OnSuccessListener<Location>() { @Override public void onSuccess(Location location) { if(location != null){ currentLocation = location; // 给currentLocation赋值 SupportMapFragment supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map); assert supportMapFragment != null; supportMapFragment.getMapAsync(MapFragment.this); } } });
内容的提问来源于stack exchange,提问作者jasagregoric
相关产品推荐
相关产品推荐

