Android Null Object Link General Preferences

I'm new to Android development, maybe this is a dumb question, but please help me. I get this error when trying to save a value of type int.

Called: java.lang.NullPointerException: attempt to call the virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences (java.lang.String, int)' to reference an empty object

And here is my code

SharedPreferences prefs = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
String value = "com.example.app.value";
int i = prefs.getInt(value, 0);

And for the record

prefs.edit().putInt(number, i).apply();

I just want to set SharedPreferences and want to read it first and write to Activity. How can I solve this?

EDIT

public class MainActivity extends Activity {
SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "myprefs";
public static final  String value = "key";

int i = sharedpreferences.getInt(value, 0);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
}
public void sendMessage(View view) {
    i += 1;    
    SharedPreferences.Editor editor = sharedpreferences.edit();
    editor.putInt(value, i);
    editor.apply();
}

I managed to save the settings in another way, but I could not read them in MainActivity, it extends the Activity class.

magazine:

: java.lang.NullPointerException: "int android.content.SharedPreferences.getInt(java.lang.String, int)"

+9
3

SharedPreferences

name SharedPreferences:

SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(this);
        Editor editor = sharedPreferences.edit();
        editor.putString(key, name);
        editor.apply();

name SharedPreferences:

 SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(this);
        String name = sharedPreferences.getString(key, "default value");

: http://developer.android.com/training/basics/data-storage/shared-preferences.html

:

public class MainActivity extends Activity {
SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "myprefs";
public static final  String value = "key";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    int i = sharedpreferences.getInt(value, 0);
    //use the value of i where needed.
}
public void saveMessage(View view) {
    i += 1;    
    SharedPreferences.Editor editor = sharedpreferences.edit();
    editor.putInt(value, i);
    editor.apply();
}
+10

public class PreferenceClass {
    public static final String PREFERENCE_NAME = "PREFERENCE_DATA";
    private final SharedPreferences sharedpreferences;

    public PreferenceClass(Context context) {
         sharedpreferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
    }

    public int getCount() {
         int count = sharedpreferences.getInt("count", -1);
         return count;
    }

    public void setCount(int count) {
         SharedPreferences.Editor editor = sharedpreferences.edit();
         editor.putInt("count", count);
         editor.commit();
    }

    public void clearCount() {
         SharedPreferences.Editor editor = sharedpreferences.edit();
         editor.remove("count");
         editor.commit();
    }
}

getCount() -1, .

+1

" RetroFit" Android. , BaseApplication Android...

0

All Articles