English 中文(简体)
收到违约分享优惠和入侵服务
原标题:getDefaultSharedPreferences and an IntentService crash

I am trying to run this but It crashes when It gets to getDefaultSharedPreferences(). Why?

这里是优惠活动。 当销毁时,发射<条码>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();
    }
}

Here is the IntentService that crashes. The line of the crash is marked with: "here it crashes!"

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);
    }
}
最佳回答

你在一个服务局的建筑中做了大量的工作。

http://www.un.org/Depts/DGACM/index_russian.htm Override #onCreate() and do You set up in which. 请打电话super.onCreate()。

问题回答

In case of IntentService, it is better to do initialization in OnHandleIntent . Also In your Case the Context may not have been initialized. So move the code in constructor to OnHandleIntent





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签