我所拥有的是Android设置菜单中的CheckBoxPreferenceEditTextPreference。我想做的是单击CheckBox时取消选中EditTextPreference。除非我尝试添加侦听器,否则一切正常。看来问题始于空的“ custFragment”。该应用程序崩溃了。这是代码:

XML格式

<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory
        android:title="@string/pref_search_method"
        android:key="prefSearchMethodSettings"
        android:layout="@layout/preferences_header_category">

        <CheckBoxPreference
            android:title="@string/use_menu"
            android:key="SHOWMENU"
            android:defaultValue="1">
        </CheckBoxPreference>

        <EditTextPreference
            android:title="@string/use_keyword"
            android:key="KEYWORD"
            android:summary="@string/use_keyword_summary"
            android:defaultValue="@string/settings_keyword_default">
        </EditTextPreference>

    </PreferenceCategory>

</PreferenceScreen>


爪哇

package com.triunedev.foodeze;

import android.app.Activity;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.util.Log;

public class SettingsActivity extends Activity {

    SettingsFragment custFragment;
    EditTextPreference textPref;
    CheckBoxPreference checkPref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Display the fragment as the main content.
        getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment(), "fragTag").commit();

        custFragment = (SettingsFragment) getFragmentManager().findFragmentByTag("fragTag");
        textPref = (EditTextPreference) custFragment.findPreference("KEYWORD");
        checkPref = (CheckBoxPreference) custFragment.findPreference("SHOWMENU");

        textPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            public boolean onPreferenceClick(Preference preference) {
                checkPref.setChecked(false);
                return false;
            }
        });
    }

}


和...

package com.triunedev.foodeze;

import android.os.Bundle;
import android.preference.PreferenceFragment;

public class SettingsFragment extends PreferenceFragment {

    public SettingsFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);
    }

}


拜托,我哪里出问题了?谢谢!

最佳答案

您的custFragment为空。

您还需要在交易期间将fragment添加到backstack

将代码替换为:

FragmentManager fm = getFragmentManager();
Fragment fragment = new SettingsFragment();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(android.R.id.content, fragment, "fragTag");
ft.addToBackStack("fragTag");
ft.commit();
fm.executePendingTransactions();


现在,使用以下代码重用该片段:

custFragment = (SettingsFragment) getFragmentManager().findFragmentByTag("fragTag");


编辑

要解决“后退”按钮中的问题:在onBackPressed()中覆盖SettingsActivity并添加以下代码:

 @Override
public void onBackPressed() {
    if (getFragmentManager.getBackStackEntryCount() > 0)
       getFragmentManager.popBackStackImmediate();
    else super.onBackPressed();
}

09-11 18:47