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);
}
}