在读取SharedPreferences时,SharedPreferences会引发FATAL异常

时间:2015-12-01 17:10:46

标签: android sharedpreferences

我正在访问SharedPreferences以获取尚未插入的值。我使用这段代码:

public long getWifiWorldTimeStamp(){
    long timestamp = defaultPreferences.getLong(TIMESTAMP_TILES_WORLD, 0);
    return timestamp;
}

我从未使用此键在SharedPreferences上写,因此,函数必须返回0(按默认值的顺序)。问题在于它抛出了这个:

12-01 18:00:04.281 6837-7661/? E/AndroidRuntime: FATAL EXCEPTION: Thread-574
 Process: com.aaaa.bbbb.debug, PID: 6837
 java.lang.InternalError: Thread starting during runtime shutdown
     at java.lang.Thread.nativeCreate(Native Method)
     at java.lang.Thread.start(Thread.java:1063)
     at org.apache.http.impl.conn.tsccm.AbstractConnPool.enableConnectionGC(AbstractConnPool.java:145)
     at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager.createConnectionPool(ThreadSafeClientConnManager.java:125)
     at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager.<init>(ThreadSafeClientConnManager.java:103)
     at org.acra.util.HttpRequest.getHttpClient(HttpRequest.java:214)
     at org.acra.util.HttpRequest.send(HttpRequest.java:141)
     at org.acra.sender.HttpSender.send(HttpSender.java:225)
     at org.acra.SendWorker.sendCrashReport(SendWorker.java:179)
     at org.acra.SendWorker.checkAndSendReports(SendWorker.java:141)
     at org.acra.SendWorker.run(SendWorker.java:77)

编辑:

这是我调用函数的地方:

@Override
public void execute(final CallbackGetAll callback) {
    long savedTimeStamp = preferences.getWifiWorldTimeStamp();
    if (preferences.getWifiWorldTimeStamp() == 0 || ((System.currentTimeMillis()) / 1000) > savedTimeStamp) {
        getFromServer(callback);
    } else {
        readWifiWorldFromFile(callback);
    }
}
编辑:我认为问题来自其他部分。在尝试从SharedProperties中读取时,似乎它会抛出一个 java.lang.ClassCastException 。 Particullary a java.lang.NumberFormatException:无效长

我不明白为什么,我在SharedPreferences中存储了一个很长的值但是当我读它时它会崩溃。我也用同样的结果尝试了int和String值。

这是我用来编写,读取和使用的代码。

存储值:

public void saveWifiWorldTimeStamp(String wifiWorldTimeStamp){
    SharedPreferences.Editor editor = defaultPreferences.edit();
    editor.putString(TIMESTAMP_TILES_WORLD, wifiWorldTimeStamp);
    editor.commit();
}

阅读价值

public String getWifiWorldTimeStamp(){
    String timestamp = defaultPreferences.getString(TIMESTAMP_TILES_WORLD, "0");
    return timestamp;
}

我需要存储值

@Override
public void execute(final CallbackGetAll callback) {
    int savedTimeStamp = Integer.parseInt(preferences.getWifiWorldTimeStamp());
    if (savedTimeStamp == 0 || Integer.parseInt(getCurrentTimestamp()) > savedTimeStamp) {
        getFromServer(callback);
    } else {
        readWifiWorldFromFile(callback);
    }
}

我在SharedPreferences中写的地方:

@Override
public void onSuccessWebService(final JSONArray response, String etag) {
    final List<WiFiGlobe> wifiGlobeList = ConverterUtils.convertJSONArrayInWifiGlobeList(response);

            saveWifiWorldToFile(response);

    preferences.saveWifiWorldTimeStamp(getTimestampToSave());
    preferences.saveEtagTilesWorld(etag);

    LogsInDebugMode.log(TAG, "getFromServer FINISH");
    callback.onSuccess(wifiGlobeList);
}

这就是我调用execute()

的方法
private void getWifiSpotsFromWorld(Location mCurrentLocation, float zoomLevel) {
    WiFiTile wifiTile = new WiFiTile();
    wifiTile.setZoomLevel((int) zoomLevel);
    wifiTile.calculateTile(mCurrentLocation);

    getWifiSpotsFromWorldInteractor.execute(wifiTile, new GetWifiSpotsFromWorldInteractorImpl.Callback() {

        @Override
        public void onSuccess(List<WiFiGlobe> wiFiGlobeList) {
            CopyOnWriteArrayList<WiFiGlobe> wifisCopyList = new CopyOnWriteArrayList<WiFiGlobe>(wiFiGlobeList);
            //TODO: Modificar la View para el método de pintado de globos
            SpotsMapPresenterImpl.wifiWorldSpotsList = wifisCopyList;
            view.showWifiWorldspots(wifisCopyList);
            view.hideProgress();
        }

        @Override
        public void onFailure(String errorText) {
            ACRA.getErrorReporter().handleSilentException(new MyEventException("Getting venues failed: " + errorText));
            view.showPopUpMessage(errorText);
            view.hideProgress();
        }

        @Override
        public void onNothingToRefresh() {
            LogsInDebugMode.error(TAG, "No need to update WifiWorld On Map");
        }
    });
}

0 个答案:

没有答案