如何使用onPreferenceClick从静态PreferenceFragment调用方法

时间:2018-07-20 13:44:58

标签: android android-fragments android-preferences

我正在使用Android Studio设置模板(该模板在AppCompatPreferenceActivity中使用静态PreferenceFragment)。我正在尝试收听首选项单击,以调用打开文件选择器的方法。 我收到一条错误消息:

非静态方法pickImage(int)不能从静态上下文中引用”。 我不确定如何正确执行。我是否应该将所有调用的方法都更改为静态方法?

代码的相关部分:

public class SettingsActivity extends AppCompatPreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupActionBar();
        setTheme(R.style.PreferencesStyle);
        // Display the fragment as the main content.
        getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new GeneralPreferenceFragment())
            .commit();
    } 

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static class GeneralPreferenceFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.pref_general);
            setHasOptionsMenu(true);            

            Preference correctAnswerPickImagePref = findPreference(Constants.CORRECT_ANSWER_PICK_IMAGE_PREF);
            correctAnswerPickImagePref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                public boolean onPreferenceClick(Preference preference) {
                    pickImage(Constants.PICK_CORRECT_ANSWER_IMAGE_REQUEST);
                    return true;
                }
            });
        }  


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            startActivity(new Intent(getActivity(), SettingsActivity.class));
            return true;
        }
        return super.onOptionsItemSelected(item);
        }
    }

    private void pickImage(int requestCode) {
        if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) {

        // Permission is not granted
        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.READ_EXTERNAL_STORAGE)) {
                // Show an explanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.
            } else {
                // No explanation needed; request the permission
                ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    requestCode);

                // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                // app-defined int constant. The callback method gets the
                // result of the request.
            }
        } else {
            // Permission has already been granted
            startPickImageActivity(requestCode);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

解决方案1: 将private void pickImage(int)方法移至GeneralPreferenceFragment,然后修复因上下文引起的错误。

解决方案2: 将private void pickImage(int)方法设为静态,并为其添加Context参数,然后修复由上下文引起的错误。

答案 1 :(得分:1)

我认为您的代码存在以下问题:

  1. 您的活动可以扩展正常的AppCompatActivity
  2. 您的GeneralPreferenceFragment不应为static
  3. pickImageGeneralPreferenceFragment内更好