1
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’m calling the Retrofit
to execute the 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);
(...)
Class Extendedactivity
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 method saveData
. I have tried to use the context of other activities and it keeps giving me error. Someone can help?
Instead of
this.getContext()
usethis
.– ramaral
In addition, this method does not even exist in the Activity class unless it has been declared in Basectivity.
– ramaral
@ramaral, I changed to this, I put a Log before and it’s not null, but I keep getting the java.lang.Nullpointerexception exception: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null Object Reference in saveData()
– porthfind
You can’t do this
ExtendedActivity ext = new ExtendedActivity();
– ramaral
@ramaral thank you
– porthfind