Activity.this不能从静态上下文引用

时间:2016-06-06 12:14:10

标签: java android android-activity android-context

嗯,这个问题类似于论坛上已经提出的问题,但我找不到合适的问题答案。我的用户选择活动类 -

public class TestRunner {

    public static void main(String[] args) {

        TestRunner r = new TestRunner();
        r.call(null);
     }

    void call(Object obj){
        System.out.println("I am from object");
    }
    void call(String obj){
        System.out.println("I am from string");
    }

    void call(Integer obj){
        System.out.println("I am from Integer");
    }
 }   

我的LanguageDialog课程 -

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

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

2 个答案:

答案 0 :(得分:3)

您可以尝试使用getActivity()而不是使用.this

答案 1 :(得分:1)

您 MyPreferenceFragment是静态的,您不能从静态片段引用非静态类 public static class MyPreferenceFragment extends PreferenceFragment从此处删除静态