我需要将基于 SharedPreferences 的preferences.xml 文件加载到 ViewPager 中。我收到此错误:
java.lang.IllegalStateException: Content view not yet created
我正在寻找解决方案,但我找不到任何东西。我能做些什么?
MainActivity.java
public class MainActivity extends FragmentActivity {
private CustomViewPager customViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
customViewPager = (CustomViewPager) findViewById(R.id.pager);
customViewPager.setAdapter(new CustomPagerAdapter());
customViewPager.setPageMargin(0);
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
}
private class CustomPagerAdapter extends PagerAdapter {
@Override
public Object instantiateItem(ViewGroup viewGroup, int position) {
RelativeLayout viewLayout = null;
LayoutInflater inflater = (LayoutInflater) viewGroup.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
switch (position) {
case 0:
viewLayout = (RelativeLayout) inflater.inflate(R.layout.list_meetings, null);
viewGroup.addView(viewLayout, 0);
break;
case 1:
viewLayout = (RelativeLayout) inflater.inflate(R.layout.settings, null);
viewGroup.addView(viewLayout, 0);
getFragmentManager().beginTransaction().replace(android.R.id.content, new FragmentPreferences()).commit();
break;
}
customViewPager.setObjectForPosition(viewLayout, position);
return viewLayout;
}
@Override
public int getCount() {
return 2;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
public static class FragmentPreferences extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// View view = inflater.inflate(R.layout.settings, null);
// return view;
return null;
}
}
}
设置.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
首选项.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:key="speed_setting"
android:title="@string/speed_setting_title"
android:summary="@string/speed_setting_description"
android:dialogTitle="@string/speed_setting_title"
android:entries="@array/speed_setting_entry_labels"
android:entryValues="@array/speed_setting_entry_labels"
android:defaultValue="@string/speed_setting_default_value"
android:icon="@drawable/ic_list_speed_setting" />
<ListPreference
android:key="permission_type_setting"
android:title="@string/permission_type_setting_title"
android:summary="@string/permission_type_setting_description"
android:dialogTitle="@string/permission_type_setting_title"
android:entries="@array/permission_type_setting_entry_labels"
android:entryValues="@array/permission_type_setting_entry_labels"
android:defaultValue="@string/permission_type_setting_default_value"
android:icon="@drawable/ic_list_permission_type_setting" />
<ListPreference
android:key="sensibility_setting"
android:title="@string/sensibility_setting_title"
android:summary="@string/sensibility_setting_description"
android:dialogTitle="@string/sensibility_setting_title"
android:entries="@array/sensibility_setting_entry_labels"
android:entryValues="@array/sensibility_setting_entry_labels"
android:defaultValue="@string/sensibility_setting_default_value"
android:icon="@drawable/ic_list_sensibility_setting" />
<ListPreference
android:key="time_format_setting"
android:title="@string/time_format_setting_title"
android:summary="@string/time_format_setting_description"
android:dialogTitle="@string/time_format_setting_title"
android:entries="@array/time_format_setting_entry_labels"
android:entryValues="@array/time_format_setting_entry_labels"
android:defaultValue="@string/time_format_setting_default_value"
android:icon="@drawable/ic_list_time_format_setting" />
<ListPreference
android:key="maps_setting"
android:title="@string/maps_setting_title"
android:summary="@string/maps_setting_description"
android:dialogTitle="@string/maps_setting_title"
android:entries="@array/maps_setting_entry_labels"
android:entryValues="@array/maps_setting_entry_labels"
android:defaultValue="@string/maps_setting_default_value"
android:icon="@drawable/ic_list_maps_setting" />
<ListPreference
android:key="resource_consumption_setting"
android:title="@string/resource_consumption_setting_title"
android:summary="@string/resource_consumption_setting_description"
android:dialogTitle="@string/resource_consumption_setting_title"
android:entries="@array/resource_consumption_setting_entry_labels"
android:entryValues="@array/resource_consumption_setting_entry_labels"
android:defaultValue="@string/resource_consumption_setting_default_value"
android:icon="@drawable/ic_list_resource_consumption_setting" />
</PreferenceScreen>