Android片段中的java Mapbox SDK
我想在片段中显示mapbox mapview,但我不能这样做。我看了很多问题和解决方案,但我不能解决我的问题。应用程序总是崩溃。BottomNavigation类是主类,MapFragment是我想查看map的片段类。我还附上了Xml代码。谢谢大家!
底部导航。爪哇
import 安卓.os.Bundle;
import 安卓.support.annotation.NonNull;
import 安卓.support.design.widget.BottomNavigationView;
import 安卓.support.v4.app.Fragment;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.view.MenuItem;
import 安卓.widget.TextView;
public class BottomNavigation extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bottom_navigation);
BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(this);
loadFragment(new MapFragment());
}
private boolean loadFragment(Fragment fragment){
if (fragment != null){
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,fragment).commit();
return true;
}
return false;
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment fragment = null;
switch (menuItem.getItemId()){
case R.id.navigation_map:
fragment = new MapFragment();
break;
case R.id.navigation_search:
fragment = new SearchFragment();
break;
case R.id.navigation_event:
fragment = new EventsFragment();
break;
case R.id.navigation_profile:
fragment = new ProfileFragment();
break;
}
return loadFragment(fragment);
}
}
地图碎片。爪哇
import 安卓.content.Context;
import 安卓.net.Uri;
import 安卓.os.Bundle;
import 安卓.support.annotation.NonNull;
import 安卓.support.annotation.Nullable;
import 安卓.support.v4.app.Fragment;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.Style;
public class MapFragment extends Fragment {
private MapView mapView;
public MapFragment(){
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map,container,false);
mapView = (MapView) view.findViewById(R.id.mapview);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(@NonNull MapboxMap mapboxMap) {
mapboxMap.setStyle(new Style.Builder().fromUrl("mapbox://styles/orucbe/cjqnneisv0gns2ro1fy83ucgl"));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(41.885, -87.679)) // set the camera's center position
.zoom(12) // set the camera's zoom level
.tilt(20) // set the camera's tilt
.build();
// Move the camera to that position
mapboxMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
});
return view;
}
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
}
活动\底部\导航。xml
<?xml version="1.0" encoding="utf-8"?>
<安卓.support.constraint.ConstraintLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:id="@+id/container"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context=".BottomNavigation">
<FrameLayout
安卓:id="@+id/fragment_container"
安卓:layout_width="0dp"
安卓:layout_height="0dp"
安卓:layout_marginBottom="56dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</FrameLayout>
<安卓.support.design.widget.BottomNavigationView
安卓:id="@+id/navigation"
安卓:layout_width="0dp"
安卓:layout_height="wrap_content"
安卓:layout_marginStart="0dp"
安卓:layout_marginEnd="0dp"
安卓:background="?安卓:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
</安卓.support.constraint.ConstraintLayout>
碎片图。xml
<?xml version="1.0" encoding="utf-8"?>
<安卓.support.constraint.ConstraintLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="match_parent"
xmlns:mapbox="http://schemas.安卓.com/apk/res-auto"
安卓:layout_height="match_parent"
>
<com.mapbox.mapboxsdk.maps.MapView
安卓:id="@+id/mapview"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
>
</com.mapbox.mapboxsdk.maps.MapView>
</安卓.support.constraint.ConstraintLayout>
# 1 楼答案
问题是没有设置的访问令牌。应在充气前设置访问令牌。就这些