Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用片段的Android SwitchPreference_Android_Android Layout - Fatal编程技术网

使用片段的Android SwitchPreference

使用片段的Android SwitchPreference,android,android-layout,Android,Android Layout,我有一个活动,用一个片段偏好来表示偏好屏幕 活动布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"

我有一个活动,用一个片段偏好来表示偏好屏幕

活动布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<fragment
    android:id="@+id/frag"
    android:name="com.test.preference.Frag"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="General">
    <SwitchPreference android:title="Enable module" android:key="asd" android:summaryOff="Turn on module" android:summaryOn="Turn off module"/>
    <SwitchPreference android:title="Post in application" android:key="qwe" android:summary="Show posts in application"/>
    <SwitchPreference android:title="Post in widget" android:key="zxc" android:summary="Show posts in widget"/>
</PreferenceCategory>
public class TestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
}
public class Frag extends PreferenceFragment{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.test);
}
}
最后是片段类:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<fragment
    android:id="@+id/frag"
    android:name="com.test.preference.Frag"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="General">
    <SwitchPreference android:title="Enable module" android:key="asd" android:summaryOff="Turn on module" android:summaryOn="Turn off module"/>
    <SwitchPreference android:title="Post in application" android:key="qwe" android:summary="Show posts in application"/>
    <SwitchPreference android:title="Post in widget" android:key="zxc" android:summary="Show posts in widget"/>
</PreferenceCategory>
public class TestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
}
public class Frag extends PreferenceFragment{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.test);
}
}
活动正常启动,但当我单击第一个SwitchPreference时,其状态保持不变,而其他两个SwitchPreference更改其状态

如果我使用不推荐的方法(PreferenceActivity,不带片段),它就可以工作

有人能帮我吗


朱利奥

我在一个片段中遇到了同样的问题。你有没有用过这个?对于那些可能发现这个的人。。。我发现了问题。我正在使用heme.Holo.Light.Dialog进行设置活动。我把它改成了android:Theme.Holo.NoActionBar。现在很好,我也有同样的问题