4

所以我正在制作一个 android 应用程序并开始处理首选项,当我简单地使用 PreferenceActivity 时,一切正常,但是,如果我将它与 PreferenceFragment 一起使用,它会在我单击调用意图的按钮后立即崩溃 SettingsActivity 与一个错误:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{*app package*/*app package*.SettingsActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f08003d for fragment SettingsFragment{41cc1a98 #0 id=0x7f08003d}

这是设置活动:

public class SettingsActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //addPreferencesFromResource(R.xml.preference);
        // Display the fragment as the main content.
        getFragmentManager().beginTransaction()
                .replace(R.id.pager, new SettingsFragment())
                .commit();
    }


    public static class SettingsFragment extends PreferenceFragment {

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

            addPreferencesFromResource(R.xml.preference);

        }
    }
}

R.id.pager 只是一个简单的 viewPager 布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

我的偏好xml:

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

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

    <CheckBoxPreference
        android:key="pref_sync"
        android:title="Sync?"
        android:summary="Do u wanna sync?"
        android:defaultValue="true" />

</PreferenceScreen>

我对android还是很陌生,所以任何帮助都会很棒!谢谢!!

编辑: 在正确答案的评论中解决了问题

4

1 回答 1

2

你必须先setContentView(R.layout.view_pager_layout)打电话

getFragmentManager().beginTransaction().replace(R.id.pager, new SettingsFragment()).commit();
于 2014-07-12T10:10:39.600 回答