Reputation: 29
Well, the question is something which is similar to already asked questions on the forum but I couldn't find a suitable answer to my problem.My usersettingactivity class-
public class UserSettingActivity extends PreferenceActivity implements View.OnClickListener,
SharedPreferences.OnSharedPreferenceChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceScreen screen = getPreferenceScreen();
PreferenceCategory langCategory=(PreferenceCategory)findPreference("change_lang");
if(PrefSettings.getInstance().getUserCountry().equalsIgnoreCase("NA"))
screen.removePreference(langCategory);
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
LayoutInflater.from(this).inflate(R.layout.user_preference_layout, root, true);
Toolbar toolbar = (Toolbar) root.findViewById(R.id.toolbar);
toolbar.setNavIconClickListener(this);
TextView version = (TextView) root.findViewById(R.id.build_version);
version.setText("v" + BuildConfig.VERSION_NAME);
getFragmentManager().beginTransaction().replace(R.id.fragment, new MyPreferenceFragment()).commit();
}
@Override
public void onClick(View v) {
finish();
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
AppTracker.btnClicked(key);
if ("prefNotification".equals(key)) {
Utils.configureServiceAlarm(this);
}
}
public static class MyPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
if (preference.getKey().contains("whitelist_paths")) {
startActivity(new Intent(getActivity(), FolderChooserActivity.class));
}
if(preference.getKey().contains("change_language"))
LanguageDialog languageDialog=new LanguageDialog(UserSettingActivity.this); //Here is the problem
languageDialog.langDialogFragment();
return true;
}
}}
My LanguageDialog class-
public class LanguageDialog {
private Activity a = null;
HashMap<String, String> lang;
public LanguageDialog(Activity activity){
a=activity;
}
public void langDialogFragment()
{
lang = new HashMap<String, String>();
lang.put("English","en");
lang.put("Bengali","bn");
lang.put("Hindi","hi");
lang.put("Kannada","kn");
lang.put("Marathi","mr");
lang.put("Tamil","ta");
lang.put("Telugu","te");
final CharSequence[] inlang = {"English","Hindi","Bengali","Kannada","Marathi","Tamil","Telugu"};
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(a, R.style.AlertDialog));
builder.setTitle(R.string.select_language)
.setIcon(R.mipmap.ic_launcher)
.setSingleChoiceItems(inlang, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String lang_code=lang.get(inlang[which].toString());
Logger.d(inlang[which].toString());
updateLocale(lang_code);
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Utils.navigateToActivity(a);
a.finish();
}
})
.show();
}
public void updateLocale(String lang)
{
Locale locale;
if(lang.equalsIgnoreCase("en")) {
locale = new Locale(lang,"US");
PrefSettings.getInstance().setUserlanguage(lang);
}
else {
locale = new Locale(lang,"IN");
PrefSettings.getInstance().setUserlanguage(lang);
}
Logger.d(lang);
Locale.setDefault(locale);
Configuration config = a.getResources().getConfiguration();
config.locale = locale;
a.getBaseContext().getResources().updateConfiguration(config,
a.getBaseContext().getResources().getDisplayMetrics());
} }
The problem is occurring in the UserSettingActivity where I am not able to create a object of LanguageDialog in the fragment class. It is showing the error - UserSettingActivity cannot be referenced from a static context. I have already tried all the possible answers to the questions similar to this problem but none could help me out.Thanks in advance for the help!
Upvotes: 0
Views: 5630
Reputation: 3388
your
MyPreferenceFragment is static, you cannot refer a non- static class from static fragment
public static class MyPreferenceFragment extends PreferenceFragment
remove static from here
Upvotes: 1