Android Preference for APIs below and above 3.0
I have a FK issue on my preference setting. I have searched the web for some preferred examples for working with APIs lower and higher than 3.0. So I got 2 preference XML files just like the tutorial. Then I used the same method to validate and call each XML settings file and now I get FC. I also had to manually change the min API to 11 from 7 in order to run sometimes, which defeats the goal. Both xmls are identical. Any insight?
Prefs Class
package com.armstrong.yi.android.app;
import java.util.List;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import com.armstrong.yi.android.app.R;
public class Prefs extends PreferenceActivity {
final static String ACTION_PREFS_ONE = "com.armstrong.yi.android.app.PREFS";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
String action = getIntent().getAction();
if (action != null && action.equals(ACTION_PREFS_ONE)) {
addPreferencesFromResource(R.xml.prefs);
}
else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
// Load the legacy preferences headers
addPreferencesFromResource(R.xml.prefs_legacy);
}
}
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.prefs, target);
}
}
prefs xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference
android:key="name"
android:summary="Enter Your Name"
android:title="EditText" />
<CheckBoxPreference
android:defaultValue="true"
android:key="checkbox"
android:summary="for the splash screen"
android:title="Music" />
<ListPreference
android:entries="@array/list"
android:key="list"
android:summary="This is a list to choose from"
android:title="list"
android:entryValues="@array/lValues" />
</PreferenceScreen>
manifesto
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.armstrong.yi.android.app"
android:versionCode="2"
android:versionName="2.7.9" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<application
android:name="GlobalVariables"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Splash"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.armstrong.yi.android.app.NumCounter"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="com.armstrong.yi.android.app.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Menu"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="com.armstrong.yi.android.app.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".TextPlay"
android:label="@string/title_activity_main" >
</activity>
<activity
android:name=".Email"
android:label="@string/title_activity_main" >
</activity>
<activity
android:name=".DailyDataEntry"
android:label="@string/title_activity_main" >
</activity>
<activity
android:name=".WeeklyDataEntry"
android:label="@string/title_activity_main" >
</activity>
<activity
android:name=".Camera"
android:label="@string/title_activity_main"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".InitialDataEntry"
android:label="@string/title_activity_main" >
</activity>
<activity
android:name=".FinalDataEntry"
android:label="@string/title_activity_main" >
</activity>
<activity
android:name=".Rankings"
android:label="@string/title_activity_main" >
</activity>
<activity
android:name=".Data"
android:label="@string/title_activity_main" >
</activity>
<activity
android:name=".OpenedClass"
android:label="@string/title_activity_main" >
</activity>
<activity
android:name=".AboutUs"
android:label="@string/title_activity_main"
android:theme="@android:style/Theme.Dialog" >
<intent-filter>
<action android:name="com.armstrong.yi.android.app.ABOUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.armstrong.yi.android.app.GlobalVariables"
android:label="@string/title_activity_main" >
</activity>
<activity
android:name=".Prefs"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="com.armstrong.yi.android.app.PREFS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Error log
01-23 17:44:40.877: W/dalvikvm(2095): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
01-23 17:44:40.957: E/AndroidRuntime(2095): FATAL EXCEPTION: main
01-23 17:44:40.957: E/AndroidRuntime(2095): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.armstrong.yi.android.app/com.armstrong.yi.android.app.Prefs}: java.lang.RuntimeException: XML document must start with <preference-headers> tag; foundPreferenceScreen at Binary XML file line #2
01-23 17:44:40.957: E/AndroidRuntime(2095): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-23 17:44:40.957: E/AndroidRuntime(2095): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-23 17:44:40.957: E/AndroidRuntime(2095): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-23 17:44:40.957: E/AndroidRuntime(2095): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-23 17:44:40.957: E/AndroidRuntime(2095): at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 17:44:40.957: E/AndroidRuntime(2095): at android.os.Looper.loop(Looper.java:137)
source to share
You need to add another file, for example pref_headers.xml
, since Android can also use headers to determine how to show the settings. See Documentation.
In it put the name of your fragment
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android" >
<header
android:fragment="com.myapp.MyFragment"
android:title="My header" />
</preference-headers>
Add tags header
for other snippets.
Then change
loadHeadersFromResource(R.xml.prefs, target);
to
loadHeadersFromResource(R.xml.pref_headers.xml, target);
source to share