如何在单击首选项时启动片段

时间:2014-03-18 02:52:01

标签: java android

我想在点击偏好项目时启动片段。请参阅以下内容:

   <PreferenceSreen
        android:key="zd_pref"
        android:summary="@string/zd_summary_no"
        android:title="@string/zd_platform" >
        <intent
            android:action="com.example.prefs.PREFS_ONE"
            android:targetClass="com.android.settings.bussiness.AuthCertifyConfirm"
            android:targetPackage="com.example.settings" />
    </PreferenceSreen>
    <PreferenceSreen
        android:key="hw_pref"
        android:summary="@string/hw_summary"
        android:title="@string/hw_platform" >
        <intent
            android:action="com.example.prefs.PREFS_ONE"
            android:targetClass="com.android.settings.bussiness.AuthCertifyConfirm"
            android:targetPackage="com.example.settings" />
    </PreferenceSreen>

AuthCertifyConfirm是一个片段,

public class AuthCertifyConfirm extends Fragment {}

但是,单击首选项时,此方法会导致错误。错误:

E/AndroidRuntime(28027): FATAL EXCEPTION: main
E/AndroidRuntime(28027): android.view.InflateException: Binary XML file line #21: Error inflating class PreferenceSreen
E/AndroidRuntime(28027):    at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:441)
E/AndroidRuntime(28027):    at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
E/AndroidRuntime(28027):    at android.preference.GenericInflater.inflate(GenericInflater.java:326)
E/AndroidRuntime(28027):    at android.preference.GenericInflater.inflate(GenericInflater.java:263)
E/AndroidRuntime(28027):    at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:269)
E/AndroidRuntime(28027):    at android.preference.PreferenceFragment.addPreferencesFromResource(PreferenceFragment.java:285)
E/AndroidRuntime(28027):    at com.android.settings.bussiness.BussinessSettings.onCreate(BussinessSettings.java:78)
E/AndroidRuntime(28027):    at android.app.Fragment.performCreate(Fragment.java:1673)
E/AndroidRuntime(28027):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:854)
E/AndroidRuntime(28027):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
E/AndroidRuntime(28027):    at android.app.BackStackRecord.run(BackStackRecord.java:682)
E/AndroidRuntime(28027):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
E/AndroidRuntime(28027):    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
E/AndroidRuntime(28027):    at android.os.Handler.handleCallback(Handler.java:725)
E/AndroidRuntime(28027):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(28027):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(28027):    at android.app.ActivityThread.main(ActivityThread.java:5041)
E/AndroidRuntime(28027):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(28027):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(28027):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
E/AndroidRuntime(28027):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
E/AndroidRuntime(28027):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(28027): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.preference.PreferenceSreen" on path: /data/app/com.android.settings-1.apk

为什么这个方法无法启动片段?

2 个答案:

答案 0 :(得分:1)

意图用于启动活动,服务,内容提供商和广播接收者。因此,如果要添加的片段与首选项片段处于同一活动中,则它不是意图的理想用例。也许尝试各种各样的东西:

public class MyPreferenceFragment extends PreferenceFragmentCompat{

   OpenFragmentListener openFragmentListener;

   public interface OpenFragmentListener{
      void openFragment();
   }

    @Override
    public void onAttach(Activity activity){
       super.onAttach(activity);
       openFragmentListener = (OpenFragmentListener)activity;

    }

    @Override
    public void onCreatePreferences(Bundle bundle, String s) {
        getPreferenceManager().setSharedPreferencesName(Constants.PREFERENCES);
        addPreferencesFromResource(R.xml.preferences);
        preference = this.findPreference("key"); 
        preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener(){
           @Override
           public boolean onPreferenceClick(Preference preference){
              openFragmentListener.openFragment();
              return true;
           }
        });
   }

}

然后在Activity中实现MyPreferenceFragment.OpenFragmentListener并覆盖类似这样的方法:

@Override
public void openFragment(){
   getSupportFragmentManager()
             .beginTransaction()
             .remove(myPreferenceFragment)
             .add(R.id.placeHolderForTheFragmentYouWant, theFragmentYouWant)
             .addToBackStack("setting")
             .commit();
}

答案 1 :(得分:1)

如果您在 2021 年来到这里,那么您很有可能正在使用 Kotlin 和导航组件。

因此您可以执行以下操作:

在您的 settings.xml 中:

<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">    
    
<Preference
            app:key="@string/fragment_settings_key"
            app:title="Go to Fragment!"/>

</PreferenceScreen>

在您的 SettingsFragment 中:

class SettingsFragment : PreferenceFragmentCompat() { 
    
        override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
            setPreferencesFromResource(R.xml.settings, rootKey)
    
            val preferenceFragment: Preference? = findPreference(getString(R.string.fragment_settings_key))
    
            preferenceFragment?.onPreferenceClickListener = Preference.OnPreferenceClickListener {
                view?.findNavController()?.navigate(R.id.MyFragment)
                true
            }
        }
  }