I'm using SharedPreferences
with Retrofit2
and notificações
. Each time the Notification is executed I get the exception " 'java.lang.String android.content.Context.getPackageName()' on a null object reference
".
I am calling Retrofit
to execute notification of a class that extend
of BroadcastReceiver
:
public class MyReceiver extends BroadcastReceiver {
public void createNotification(Context context, String exemplo) {
ExtendedActivity ext = new ExtendedActivity();
ext.start(exemplo);
(...)
ExtendedActivity Class
public class ExtendedActivity extends BaseActivity implements Callback<Channel>{
String descript="descript";
@Override
public void start(String texto) {
(...)
}
@Override
public void onResponse(Call<Channel> call, Response<Channel> response) {
if (response.isSuccessful()) {
Channel rss = response.body();
saveData(descript, rss.getItemDescription()); <--------- ERRO
}
}
public void saveData(String key, String time){
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.getContext()); <-----ERRO
SharedPreferences.Editor editor = preferences.edit();
if (preferences.contains(key)) {
editor.remove(key);
}
editor.putString(key, time);
editor.commit();
}
The problem is in the context of the saveData
method. I have tried to use the context of other activities and it always gives me error. Can anyone help?