Salman
Salman

Reputation: 21

SharedPreference in non-activity-class or RecyclerView adapter class

I have saved sharedpreferences on a settings activity and able to fetch it on load. Also i am able to fetch the same shared preferences in the main activity. However when i am trying to call it on a separate class that i intend to use on a recyclerview adapter so like a prefix in every card view element, i am getting a null error. Below are the challenges:

  1. I am facing difficulty in setting up a correct context.
  2. I tried creating a getter in the preference class/activity which returns null when called from a separate java class.
  3. I tried defining a context in the adapter class and using this as follows

public SharedPreferences sharedPreferences;
      public Context context;

      ...

sharedPreferences=PreferenceManager.getDefaultSharedPreferences(this.context);    
     userCurrency = sharedPreferences.getString("sPrefix","");

Also tried:

sharedPreferences=PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()); 

I am getting following error when this sharedpreference line is called.

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference

Please can you help me how to either setup a getter that i can call from a separate class, or initialize and get values in this separate class directly.

Main activity

public class MainActivity extends AppCompatActivity {

Button btnm, btnw, btnsep;
Intent intent;
TextView tvRecall;
Third third;
Shared shared;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnm = findViewById(R.id.btnmain);
    btnw = findViewById(R.id.btnwhat);
    btnsep = findViewById(R.id.sepClass);
    tvRecall=findViewById(R.id.txtRecall);

    intent = new Intent(this, Shared.class);

    btnm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            startActivity(intent);

        }
    });


    btnw.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

           SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            String s = sharedPreferences.getString("txt","");
            Log.i("testlog*****","t "+ s);
            Toast.makeText(getApplicationContext(),"Text is " + s,Toast.LENGTH_SHORT).show();

        }
    });

    btnsep.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           third = new Third();
           third.callSharedPreference();
        }
    });

}

Activity to save shared pref

public class Shared extends AppCompatActivity {

EditText textView ;
Button btn;
SharedPreferences sharedPreferences;
Context c;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shared);
    textView = findViewById(R.id.txtName);
    btn = findViewById(R.id.btnName);



    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            sharedPreferences.edit().putString("txt",textView.getText().toString()).apply();
        }
    });


}

public Shared () {    }

public void getlog () {
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(c);
        String s = sharedPreferences.getString("txt","");
        Log.i("testlog*****","t "+ s);
        Toast.makeText(getApplicationContext(),"Text is " + s,Toast.LENGTH_SHORT).show();
}

Third Class to call the sharedpreference giving null error as above when called

public class Third {

Context context;
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
String s = sharedPreferences.getString("txt","");


public Third (){}

public void callSharedPreference () {

    Log.i("testlog*****","t "+ s);
    Toast.makeText(context.getApplicationContext(),"Text is ==== " + s,Toast.LENGTH_SHORT).show();

}

Upvotes: 1

Views: 498

Answers (1)

Falah H. Abbas
Falah H. Abbas

Reputation: 512

in the class Third the context is not initialized so you should initialize it in the constructor method and pass the value to it when you call the class... like this

public class Third {
Context context;
SharedPreferences sharedPreferences = 
PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
String s = sharedPreferences.getString("txt","");


public Third (Context context){
    this.context=context
}
//...
}

Upvotes: 1

Related Questions