Reputation: 25
I want to save a sharedpreference when the application received sms like 'ON'. But then, when the device receive the sms 'ON' while I'm not using it, I've got a dialog box caption application stopped. But when I remove the sharedprefences inside the method the toast message appear so it execute the method. I think there's a problem with sharedpreferences inside that method. Please help. Thanks.
here is some code from the activity
public class MainActivity extends Activity{
static SharedPreferenceas sp;
public static void SPlron(Context context, Intent intent){
Editor edit = sp.edit();
edit.putString("a", "ON");
edit.commit();
Toast.makeText(context, "Saved!", Toast.LENGTH_SHORT).show
}
}
and i got this error in logcat
Tag: trace Text: error opening trace file: No such file or directory (2)
Upvotes: 0
Views: 70
Reputation: 1645
When your SPlron()
method is called at that time might be SharedPreferences
is not initialized. try the below code.
public class MainActivity extends Activity {
SharedPreferences preferences;
SharedPreferences.Editor editor;
public void SPlron(Context context, Intent intent){
preferences = getSharedPreferences(PREFS_NAME, 0);
editor = preferences.edit();
editor.putString("a", "ON");
editor.commit();
Log.d("Saved!","Saved");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
}
}
Let me know if more help required.
Upvotes: 1