i'm trying to update my ListPreference summary inside my PreferenceFragment with timer while i'm inside my fragment everything is fine but as soon as i press the back button my app crashes and it throws a null pointer exception saying getActivity() retured null. i also tried suppressing it by setting the timer only when getActivity() won't return null but it did'nt work.
here's my code:
public class ButtonsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.misc_option_settings, rootKey);
if (getActivity() == null)
return;
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
ListPreference rows = findPreference("btn_rows");
rows.setSummary("current button rows: " rows.getValue());
ListPreference color = findPreference("btn_colors");
color.setSummary("current button color: " color.getEntry());
}
});
}
}, 0, 100);
}
}
would appreciate if you could help.
CodePudding user response:
You should check for activity not to be null inside run
method. check for activity not to be null before calling getActivity().runOnUiThread
.
CodePudding user response:
thanks it finally worked. don't know how did i miss that.