简体   繁体   中英

Google Play Console Crash Report on preference manager in on response method of Activity

Google Play Console Crash Report Error:

java.lang.NullPointerException: 
    at in.abc.abc.abc.abc.Event_List$2.onResponse (Event_List.java:119)
      at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run (ExecutorCallAdapterFactory.java:70)
      at android.os.Handler.handleCallback (Handler.java:794)
      at android.os.Handler.dispatchMessage (Handler.java:99)
      at android.os.Looper.loop (Looper.java:176)
      at android.app.ActivityThread.main (ActivityThread.java:6635)
      at java.lang.reflect.Method.invoke (Native Method)
      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:547)
      at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:823)

Events Activity:

PrefManager prefManager;

  @Override
protected void onCreate(Bundle savedInstanceState)
{
    prefManager = new PrefManager(Event_List.this);

    getAllEvents();
}

private void getAllEvents()
{       
        getAllEvents.enqueue(new Callback<Events>() {
            @Override
            public void onResponse(Call<Events> call, Response<Events> response)
            {
                if(!response.isSuccessful())
                {
                    if (response.code() == 400 || response.code() == 401 )
                    {
                        prefManager.clear(); 
                    }

                    Toast.makeText(Event_List.this, "Please try again", Toast.LENGTH_SHORT).show();
                    return;
                }
            }

        });
    }
}

Getting java.lang.NullPointerException for clear(); method in above code.

preference manager Code:

public static SharedPreferences sharedPreferences;

public PrefManager(Context context)
{
    this.context = context;
    sharedPreferences = context.getSharedPreferences(STORE_FILE_NAME, Context.MODE_PRIVATE);
    editor = sharedPreferences.edit();
}   

public void clear()
{
    editor.clear();
    editor.commit();
}

i'm getting this error for pref manager in google play console at run time. can anyone please suggest proper way to use preference manager in project?

Creating the class:

public class MySharedPref {
    public static SharedPreferences sharedPreferences;
    Context context;
    SharedPreferences.Editor editor;

    public String getCountry() {
        return sharedPreferences.getString("COUNTRY","");
    }

    public void setCountry(String country) {
        this.country = country;
        insert("COUNTRY",country);
    }
// Instantiate constructor
    public MySharedPref(Context context)
    {
        this.context = context;
        sharedPreferences = context.getSharedPreferences("MY_PREF", Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
    }

    public void insert(String key, String value) {
        editor.putString(key, value);
        editor.apply();
    }

    public void clear()
    {
        editor.clear();
        editor.commit();
    }
}

MainActivity :

    MySharedPref sharedPref;
    String country = "";
    private static final String[] COUNTRIES = new String[] {
        "Belgium", "France", "Italy", "Germany", "Spain"};
     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            sharedPref = new MySharedPref(MainActivity.this);
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, COUNTRIES);
        AutoCompleteTextView textView = (AutoCompleteTextView)
                findViewById(R.id.countries_list);
        textView.setAdapter(adapter);
    materialButton = findViewById(R.id.materialButton);
    if (sharedPref.getCountry()!=null) {
                textView.setText(sharedPref.getCountry());
            }
textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String selection = (String) adapterView.getItemAtPosition(i);
                sharedPref.setCountry(selection);
  }
        });
    materialButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    sharedPref.clear();
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM