好吧,这个问题类似于论坛上已经问过的问题,但是我找不到适合我问题的答案。我的usersettingactivity类-

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;
    }
}}


我的LanguageDialog类-

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());


} }


我无法在片段类中创建LanguageDialog对象的UserSettingActivity中出现了问题。它显示错误-
无法从静态上下文引用UserSettingActivity。我已经尝试过解决所有与该问题类似的问题的所有可能答案,但是没有一个可以帮助我。谢谢您的帮助!

最佳答案

您可以尝试使用getActivity()代替.this

10-07 18:19