getDefaultSharedPreferences和IntentService崩溃

时间:2012-01-12 13:14:56

标签: java android crash preferences intentservice

我正在尝试运行此功能但是当它到达getDefaultSharedPreferences()时它会崩溃。 为什么?

以下是偏好活动。它在被销毁时触发IntentService

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

public class CCTDetectorActivity extends PreferenceActivity implements
        OnSharedPreferenceChangeListener {

    private SharedPreferences settings;
    static public String nameOfFile = "name_of_file";
    static public String nameOfFileDefaultValue = "detected_f.xml";
    static public String portNumber = "port_number";
    static public String portNumberDefaultValue = "25015";
    static public String keepAlive = "keep_alive";
    static public String keepAliveDefaultValue = "3";
    static public String nameOfSettings = "settings";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.layout.cct_detector_preferences_ui);
        settings = PreferenceManager.getDefaultSharedPreferences(this);
        settings.registerOnSharedPreferenceChangeListener(this);
        updateViews();
    }

    @Override
    protected void onDestroy() {
        Intent intent = new Intent(getBaseContext(), CCTDetectorService.class);
        startService(intent);
        super.onDestroy();
    }

    private void updateViews() {
        setSummeryfromPreferencesView(nameOfFile, nameOfFileDefaultValue);
        setSummeryfromPreferencesView(portNumber, portNumberDefaultValue);
        setSummeryfromPreferencesView(keepAlive, keepAliveDefaultValue);
    }

    private void setSummeryfromPreferencesView(String viewName, String DefValue) {
        String value = settings.getString(viewName, DefValue);
        EditTextPreference editTextView = (EditTextPreference) findPreference(viewName);
        editTextView.setText(value);
        editTextView.setSummary(value);
    }

    public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1) {
        updateViews();
    }
}

这是崩溃的IntentService。 崩溃线标有:“这里崩溃!”

import android.app.IntentService;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

public class CCTDetectorService extends IntentService {

    private File serializedXmlFile;
    private DatagramSocket udpSocket;
    private boolean m_Listening = true;
    private ActiveCCTs activeCCTs = new ActiveCCTs();
    private SharedPreferences preferences;
    private Serializer serializer = new Persister();

    public CCTDetectorService() throws SocketException {
        super("CCTDetectorServiceThread");
        int port;
        String FILENAME;
        // here it crashes!
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        port = getIntFromSettingsEditText(CCTDetectorActivity.portNumber,
                CCTDetectorActivity.portNumberDefaultValue);
        activeCCTs.keepAlive = getIntFromSettingsEditText(
                CCTDetectorActivity.keepAlive,
                CCTDetectorActivity.keepAliveDefaultValue);
        FILENAME = preferences.getString(CCTDetectorActivity.nameOfFile,
                CCTDetectorActivity.nameOfFileDefaultValue);
        serializedXmlFile = new File(FILENAME);
        udpSocket = new DatagramSocket(port);
        udpSocket.setBroadcast(true);
    }
}

2 个答案:

答案 0 :(得分:8)

你在服务的构造函数中做了很多事情。

这样做。覆盖#onCreate()并在那里进行设置。请记得致电super.onCreate()

答案 1 :(得分:2)

对于IntentService,最好在OnHandleIntent中进行初始化。 此外,在您的情况下,上下文可能尚未初始化。因此,将构造函数中的代码移动到OnHandleIntent