I have created an activity in which I implemented a recycler view adapter. I have also created a second activity which should add a value into a SharedPreferences
. After that, I want to change the text of a textView based on value of that SharedPreferences
. But when I try to set text of that it doesn't work, it doesn't even add a default value of SharedPreferences
.
The code inside of a activity which saves the data on click:
public void Done(View view){
SharedPreferences mPreferences =
mContext.getSharedPreferences("Checker",MODE_PRIVATE);
SharedPreferences.Editor mEditor = mPreferences.edit();
mEditor.putString("Day1", "complete");
mEditor.apply();
//mRecyclerView.setAdapter(fitnessAdapter);
fitnessAdapter.notifyDataSetChanged();
The code inside of a recycler view adapter: (just the ViewHolder where I use the code)
public class MultiAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private OnNoteListener mOnNoteListener;
private Context context;
public MultiAdapter(List<myObject> Parts, OnNoteListener onNoteListener, Context context) {
this.items = Parts;
this.mOnNoteListener = onNoteListener;
this.context = context;}
private Context context;
public class MainViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView segment;
OnNoteListener mOnNoteListener;
private MainViewHolder( View itemView, OnNoteListener onNoteListener, Context context) {
super(itemView);
segment = itemView.findViewById(R.id.segment_shredmas);
SharedPreferences mPreferences = context.getSharedPreferences("Checker",MODE_PRIVATE);
String daySelector = mPreferences.getString("Day1","Nothing");
segment.setText("Day "+ daySelector);
this.mOnNoteListener = onNoteListener;
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
mOnNoteListener.onNoteClick(getAdapterPosition());
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.segment_shredmas, parent, false);
return new ShredmasViewHolder(view, mOnNoteListener,[position] 1, [context]context);
PS : I tried both ways of SharedPreferences
. But since the old one was the last one I left the code like that
Error messege :
E/MemoryLeakMonitorManager: MemoryLeakMonitor.jar is not exist!
E/AwareLog: AtomicFileUtils: readFileLines file not exist: android.util.AtomicFile@95326f7
E/: [ZeroHung]zrhung_get_config: Get config failed for wp[0x0102]
W/ZrHung.AppEyeUiProbe: Failed to get config from zrhung
Process: com.example....., PID: 15963
java.lang.RuntimeException: Unable to pause activity {com.example...../com.example.....MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.media.MediaPlayer.getCurrentPosition()' on a null object reference
at android.app.ActivityThread.performPauseActivityIfNeeded(ActivityThread.java:4742)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:4691)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:4626)
at android.app.servertransaction.PauseActivityItem.execute(PauseActivityItem.java:45)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199)
at android.os.Handler.dispatchMessage(Handler.java:112)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7625)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.media.MediaPlayer.getCurrentPosition()' on a null object reference
at com.example.....MainActivity.onPause(MainActivity.java:131)
at android.app.Activity.performPause(Activity.java:7663)
at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1536)
at android.app.ActivityThread.performPauseActivityIfNeeded(ActivityThread.java:4726)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:4691)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:4626)
at android.app.servertransaction.PauseActivityItem.execute(PauseActivityItem.java:45)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199)
at android.os.Handler.dispatchMessage(Handler.java:112)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7625)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
E/: [ZeroHung]zrhung_get_config: Get config failed for wp[0x0008]
EDIT : so I followed its track and it led me to another activity's media player for some reason, but when I deleted that and rerun the app another NullPointer PopedUp :
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference so the problem is in the line
so the problem is in the line SharedPreferences mPreferences = mContext.getSharedPreferences("Checker",MODE_PRIVATE);
I know that the mContext is the one with nullException but when I wont use a Context in the code it will save it into different SharedPrefences. And when I want to load some SharedPreferences in recyclerView I have to reference context as I did in the code showed above so it will be in the group.