适用于API的Android Preference低于和高于3.0

时间:2013-01-23 18:00:04

标签: java android api preference runtimeexception

我的偏好设置中存在FC问题。我在网上查找了一些偏好示例,适用于低于和高于3.0的API。所以我得到了2 xml偏好文件,就像教程一样。然后我使用相同的方法检查并调用每个xml首选项文件,现在获取FC。我还必须手动将min API从7更改为11到有时运行失败的目的。两个xmls都是相同的。有什么见解吗?

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>

清单

    <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>

错误日志

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)

1 个答案:

答案 0 :(得分:0)

我解决了:

Intent intent = new Intent(this, ActivityPreferences.class);
intent.putExtra(ActivityPreferences.EXTRA_NO_HEADERS, true );   
intent.putExtra(ActivityPreferences.EXTRA_SHOW_FRAGMENT,FragmentPreferences.class.getName());
startActivity(intent);