Please, help me. I dont know what is wrong with this code:
import android.appwidget.AppWidgetProvider;
import android.content.SharedPreferences;
public class WeatherWidget extends AppWidgetProvider {
static SharedPreferences settings = getSharedPreferences("weather_prefs", 0);
public void onUpdate()
{
settings.getString("location", "N/A");
}
}
In the line “static SharedPreferences…” I get an error:
The method getSharedPreferences(String, int) is undefined for the type WeatherWidget
Why its undefined method if its class method?
The
getSharedPreferencesmethod is not available for anAppWidgetProviderbecause it’s not a Context. This link explains a bit more: Get preferences in AppWidget Provider