I don't understand why this is happening
I Mention /Error Here in the code so you can easily find out
Any idea how to fix the issue? Sometimes it works as normal without any error but some times it throws NullPointerException
Error Log
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
at com.app.android.Provider.PrefManager.<init>(PrefManager.java:34)
at com.app.android.ui.fragement.PlayerFragment.addView(PlayerFragment.java:1901)
at com.app.android.ui.fragement.PlayerFragment$25.onPlayerStateChanged(PlayerFragment.java:993)
PrefManager.java
public class PrefManager {
SharedPreferences pref;
SharedPreferences.Editor editor;
Context _context;
// shared pref mode
int PRIVATE_MODE = 0;
// Shared preferences file name
private static final String PREF_NAME = "status_app";
private static final String IS_FIRST_TIME_LAUNCH = "IsFirstTimeLaunch";
public PrefManager(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);//Error Here
editor = pref.edit();
editor.apply();
}
public void setBoolean(String PREF_NAME,Boolean val) {
editor.putBoolean(PREF_NAME, val);
editor.commit();
}
public void setString(String PREF_NAME,String VAL) {
editor.putString(PREF_NAME, VAL);
editor.commit();
}
public void setInt(String PREF_NAME,int VAL) {
editor.putInt(PREF_NAME, VAL);
editor.commit();
}
public boolean getBoolean(String PREF_NAME) {
return pref.getBoolean(PREF_NAME,true);
}
public void remove(String PREF_NAME){
if(pref.contains(PREF_NAME)){
editor.remove(PREF_NAME);
editor.commit();
}
}
public String getString(String PREF_NAME) {
if(pref.contains(PREF_NAME)){
return pref.getString(PREF_NAME,null);
}
if (PREF_NAME.equals("LANGUAGE_DEFAULT")){
return "0";
}
if(PREF_NAME.equals("ORDER_DEFAULT_IMAGE")){
return "created";
}
if(PREF_NAME.equals("ORDER_DEFAULT_GIF")){
return "created";
}
if(PREF_NAME.equals("ORDER_DEFAULT_VIDEO")){
return "created";
}
if(PREF_NAME.equals("ORDER_DEFAULT_JOKE")){
return "created";
}
if(PREF_NAME.equals("ORDER_DEFAULT_STATUS")){
return "created";
}
return "";
}
public int getInt(String key) {
return pref.getInt(key,0);
}
}
PlayerFragment.java
final PrefManager prefManager = new PrefManager(getActivity()); //Error Here
Integer id_user = 0;
String key_user= "";
if (prefManager.getString("LOGGED").toString().equals("TRUE")) {
id_user= Integer.parseInt(prefManager.getString("ID_USER"));
key_user= prefManager.getString("TOKEN_USER");
}
PlayerFragment.java
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
if (playbackState == ExoPlayer.STATE_READY) {
simple_arc_loader_exo.setVisibility(View.GONE);
exo_pause.setVisibility(View.GONE);
exo_play.setVisibility(View.GONE);
playing = true;
exo_play.setColorFilter(Color.WHITE, android.graphics.PorterDuff.Mode.SRC_IN);
addView();// Error Here
}
if (playbackState == ExoPlayer.STATE_BUFFERING) {
simple_arc_loader_exo.setVisibility(View.VISIBLE);
exo_pause.setVisibility(View.GONE);
exo_play.setVisibility(View.GONE);
}
}
@snachmsm @Bek @ADM @akhilnair Thanks for the reply I finally font out the error it on the Exo player in the lifecycle
`@Override
public void onPause() {
super.onPause();
if (Util.SDK_INT > 23) {
Log.v("VideoPlayer","onPause");
releasePlayer();
}
releasePlayer();// add this solve the error same line in onstop also
}`