Java 使用服务时,致命信号11(SIGSEGV)代码=2
在我的应用程序中,我使用的是后台服务, 这是它的基本代码Java 使用服务时,致命信号11(SIGSEGV)代码=2,java,android,android-studio,Java,Android,Android Studio,在我的应用程序中,我使用的是后台服务, 这是它的基本代码 <application android:allowBackup="true" android:vmSafeMode="true" android:allowClearUserData="true" android:hardwareAccelerated="true" android:largeHeap="true" android:i
<application
android:allowBackup="true"
android:vmSafeMode="true"
android:allowClearUserData="true"
android:hardwareAccelerated="true"
android:largeHeap="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:name=".MyApplication"
android:theme="@style/AppTheme" >
Service contains some timers
public class ConnectionService extends Service
{
private final IBinder mBinder = new ConnectionServiceBinder();
private MyApplication app;
private CountDownTimer mAliveTimer;
public CountDownTimer mPanicTimer;
public CountDownTimer mPerodicTimer;
/* Not using binder anywhere right now */
public class ConnectionServiceBinder extends Binder
{
public ConnectionService getService()
{
return ConnectionService.this;
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
//connect();
return START_STICKY;
}
public void createPerodicTimer()
{
if (mPerodicTimer != null)
mPerodicTimer.cancel();
float fastestInterval = Preferences.getFloatSharedPrefValue(app, Constants.USERDEFAULT_SETTINGS_TIME, 5f) * 60 * 1000;
mPerodicTimer = new CountDownTimer((int)fastestInterval, 1000)
{
@Override
public void onTick(long millisUntilFinished)
{}
@Override
public void onFinish()
{
Location myLocation = app.myLocationProvider.getMyLocation();
Location currentBestLocation = null;
String locValue = Preferences.getSharedPrefValue(app, Constants.USERDEFAULT_LOCATION);
if (locValue != null)
{
currentBestLocation = new Gson().fromJson(locValue, Location.class);
}
if (currentBestLocation == null)
{
sendPerodicReporting();
Preferences.saveSharedPrefValue(app, Constants.USERDEFAULT_LOCATION , new Gson().toJson(myLocation));
}
else
{
float displacement = Preferences.getFloatSharedPrefValue(app, Constants.USERDEFAULT_SETTINGS_DISTANCE, 50);
float dist = myLocation.distanceTo(currentBestLocation);
if (dist >= displacement)
{
sendPerodicReporting();
}
else
{
ApiManager.getInstance().mDbHelper.addToLog("System Event", "Perodic Report not sent", "Distance travelled is less than set displacement settings" + dist + " < " + displacement, app);
}
}
Preferences.saveSharedPrefValue(app, Constants.USERDEFAULT_LOCATION , new Gson().toJson(myLocation));
mPerodicTimer.start();
}
};
mPerodicTimer.start();
}
@Override
public void onCreate()
{
super.onCreate();
app = (MyApplication) getApplication();
app.mService = this;
app.addLocationUpdates();
createPerodicTimer();
mAliveTimer = new CountDownTimer(30000 * 60, 1000) // Keep Alive Timer is 30 minutes
{
@Override
public void onTick(long millisUntilFinished)
{}
@Override
public void onFinish()
{
if (Preferences.getBoolSharedPrefValue(app.getApplicationContext(), Constants.USERDEFAULT_SETTINGS_ALIVE, false))
sendAliveResponse();
if (Preferences.getBoolSharedPrefValue(app.getApplicationContext(), Constants.USERDEFAULT_IS_PANIC_ENABLED, false))
sendPanicReporting();
mAliveTimer.start();
}
};
mPanicTimer = new CountDownTimer(30000, 1000) // Keep Alive Timer is 30 seconds
{
@Override
public void onTick(long millisUntilFinished)
{}
@Override
public void onFinish()
{
if (Preferences.getBoolSharedPrefValue(app.getApplicationContext(), Constants.USERDEFAULT_IS_PANIC_ENABLED, false))
{
sendPanicReporting();
mPanicTimer.start();
}
}
};
mAliveTimer.start();
}
@Override
public void onDestroy()
{
super.onDestroy();
}
@Override
public IBinder onBind(Intent arg0)
{
return mBinder;
}
}
我不确定(在没有完整堆栈跟踪的情况下,有点难弄清楚发生了什么),但请尝试禁用在服务中保存共享首选项,并检查应用程序是否失败。我认为这个问题可能是由Gson序列化产生的字符串引起的
我检查了第行返回的JsonElement的实现
new Gson().toJson(...)
而且它似乎没有实现java.io.Serializable接口。这可能会导致对象保存期间出现问题 请发布完整的错误,如果需要,请堆叠available@SagiLow请检查是否没有更多信息?您是否尝试添加一些日志打印以查看它发生在何处?是的,这正是问题所在,虽然我以前已经修复了它,但供将来任何人参考:)。如果我调试它,它会工作,但不确定为什么有时会失败