所以我正在创建一个Android应用程序而且我开始处理首选项,当我只使用PreferenceActivity时,一切都很完美,但是,如果我将它与PreferenceFragment一起使用,它会在我单击按钮后立即崩溃调用Intent到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}
这是SettingsActivity:
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还很新,所以任何帮助都会很棒!!谢谢!
修改 在正确答案的评论中解决了问题
答案 0 :(得分:0)
您必须在
之前致电setContentView(R.layout.view_pager_layout)
getFragmentManager().beginTransaction().replace(R.id.pager, new SettingsFragment()).commit();