我正在使用mapbox sdk测试应用,而不是mapbox的位置引擎提供商我创建了一个名为" GoogleLocationEngine"使用此链接跟踪用户位置(Google API): https://github.com/mapbox/mapbox-android-demo/issues/449
但应用程序给了我这个错误(通常是在后台):
A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x8 in tid 18372
虽然我使用mapbox sdk:
LocationEngineProvider(context).obtainBestLocationEngineAvailable()
方法,错误仍然显示,我不知道如何跟踪它,所以我该如何解决这个问题?我的设备是SAMSUNG Galaxy Note 4(SM-N900C) 这是我的MainActivity.java:
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationResult;
import com.mapbox.android.core.location.LocationEngine;
import com.mapbox.android.core.location.LocationEngineListener;
import com.mapbox.android.core.location.LocationEnginePriority;
import com.mapbox.android.core.permissions.PermissionsListener;
import com.mapbox.android.core.permissions.PermissionsManager;
import com.mapbox.mapboxsdk.Mapbox;
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.plugins.locationlayer.LocationLayerPlugin;
import com.mapbox.mapboxsdk.plugins.locationlayer.modes.CameraMode;
import com.mapbox.mapboxsdk.plugins.locationlayer.modes.RenderMode;
import java.util.List;
public class MainActivity extends AppCompatActivity implements
LocationEngineListener,
PermissionsListener{
private MapView mapView;
private MapboxMap map;
private PermissionsManager permissionsManager;
private LocationEngine locationEngine;
private Location originLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Mapbox.getInstance(this, getString(R.string.access_token));
setContentView(R.layout.activity_main);
mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(mapboxMap -> {
map = mapboxMap;
enableLocation();
});
}
private void enableLocation(){
if(PermissionsManager.areLocationPermissionsGranted(this)){
initializeLocationEngine();
initializeLocationLayer();
} else {
permissionsManager = new PermissionsManager(this);
permissionsManager.requestLocationPermissions(this);
}
}
@SuppressWarnings("MissingPermission")
private void initializeLocationEngine(){
locationEngine = GoogleLocationEngine.getLocationEngine(this);
locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
locationEngine.activate();
Location lastLocation = locationEngine.getLastLocation();
if(lastLocation != null){
originLocation = lastLocation;
setCameraPosition(lastLocation);
} else {
locationEngine.addLocationEngineListener(this);
}
}
@SuppressWarnings("MissingPermission")
private void initializeLocationLayer(){
LocationLayerPlugin locationLayerPlugin = new LocationLayerPlugin(mapView, map, locationEngine);
locationLayerPlugin.setLocationLayerEnabled(true);
locationLayerPlugin.setCameraMode(CameraMode.TRACKING);
locationLayerPlugin.setRenderMode(RenderMode.NORMAL);
}
private void setCameraPosition(Location location){
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude() , location.getLongitude()) , 13.0));
}
@SuppressWarnings("MissingPermission")
@Override
public void onConnected() {
Log.d("onConnected" , "is Connected");
locationEngine.requestLocationUpdates();
}
@Override
public void onLocationChanged(Location location) {
if(location != null){
originLocation = location;
setCameraPosition(location);
} else
Toast.makeText(this, "Location is null", Toast.LENGTH_SHORT).show();
}
@Override
public void onExplanationNeeded(List<String> permissionsToExplain) {
//present a toast or a dialog
Toast.makeText(this, permissionsToExplain.get(0) , Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionResult(boolean granted) {
if(granted){
enableLocation();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
permissionsManager.onRequestPermissionsResult(requestCode , permissions , grantResults);
}
@Override
public void onStart() {
super.onStart();
mapView.onStart();
}
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onStop() {
super.onStop();
mapView.onStop();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
}
更新:最近我意识到了方法
locationEngine = new LocationEngineProvider(this).obtainBestLocationEngineAvailable();
无效!我认为这种方法适合我,但当我评论它并再次测试我的应用程序时它没有显示我的位置,当我使用我的GoogleLocationEngine类时,它工作但仍然保持相同的错误。 亲爱的朋友们,有什么想法吗?
答案 0 :(得分:2)
问题已经解决了! 基于这一段:
在各自的活动方法中包含位置层onStart()和onStop()生命周期事件非常重要。这可以防止发生内存泄漏并减少电池消耗。该插件支持新的LifecycleObserver API,通过在活动中添加插件作为生命周期观察器,您不需要手动处理生命周期。
来源链接:mapbox_locationLayer
和这个mapboxDemo_LocationLayerPluginActivity.java (GitHub),我应该在我的函数中使用这些方法:
<android.support.constraint.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">
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="320dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/com_facebook_profile_picture_blank_portrait" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@id/imageView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/imageView"
app:srcCompat="@android:color/holo_green_light" />
</android.support.constraint.ConstraintLayout>
和我的onStart():
locationPlugin = new LocationLayerPlugin(mapView, mapboxMap, locationEngine);
locationPlugin.setLocationLayerEnabled(true);
locationPlugin.setCameraMode(CameraMode.TRACKING);
locationPlugin.setRenderMode(RenderMode.COMPASS);
Lifecycle lifecycle = getLifecycle();
lifecycle.addObserver(locationPlugin);
也在我的onStop()中:
@Override
public void onStart() {
super.onStart();
mapView.onStart();
if(locationEngine != null){
locationEngine.requestLocationUpdates();
locationEngine.addLocationEngineListener(this);
}
}
然后致命信号错误现在消失了。
我必须说谢谢mapbox支持团队支持我并解决这个问题。