android 偏好设置问题和需要帮助

发布于 2024-10-15 19:53:18 字数 14590 浏览 6 评论 0原文

关于我的实施的另一个问题。我对 java/android 相当陌生,来自 ac/c++ 背景。我已经在我的代码中实现了这个,我只是遇到了一些问题,并且还想知道这是否是最好的解决方案以及是否有人有任何建议或可以帮助我解决我的问题。这是问题和我的问题:

我的 Android 应用程序需要有 加载设置的功能 来自 xml 文件和应用程序 需要允许用户更改 这些设置并记住 更改设置。我也需要我的 应用程序引用这些更改 并检测用户何时发生变化 设置之一。

我目前正在执行上述操作,只是我希望从 xml 文件加载默认设置,而不是代码中使用的方法(查看代码以了解我在说什么)。我已经四处寻找这个问题,似乎找不到解决方案。

一旦检测到更改,我就需要它 将值加载到我的 DataRobot 类中 变量。我有访问器功能 为此(参见代码)。然而,当我 尝试更改任何设置 它不会改变我的价值 数据机器人类。

基本上,这两个引用块解释了我需要什么。我目前正在使用 android 首选项来解决这个问题。这是解决这个问题的最好方法吗?

如果您需要任何其他信息,请告诉我。感谢您提前提供的帮助,非常感谢!


另外,我目前在当前设置中遇到了一些错误,下面列出的是错误发生时间以及所述错误的日志目录的解释。

下面的 log cat 输出发生在应用程序首次启动时。似乎在转换字符串时遇到问题并引发了 ClassCastException。我不确定这种情况发生在哪里或为什么发生。所有代码都在帖子底部。

02-03 20:05:50.731: INFO/System.out(279): debugger has settled (1384)
02-03 20:05:52.080: INFO/ActivityManager(59): Displayed activity com.android.launcher/com.android.launcher2.Launcher: 38755 ms (total 38755 ms)
02-03 20:05:52.480: WARN/Resources(279): Converting to string: TypedValue{t=0x10/d=0x3c a=-1}
02-03 20:05:52.669: WARN/System.err(279): java.lang.ClassCastException: java.lang.String
02-03 20:05:52.689: WARN/System.err(279):     at android.app.ContextImpl$SharedPreferencesImpl.getInt(ContextImpl.java:2706)
02-03 20:05:52.689: WARN/System.err(279):     at cpe495.smartapp.SmartApp.onCreate(SmartApp.java:61)
02-03 20:05:52.701: WARN/System.err(279):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-03 20:05:52.715: WARN/System.err(279):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-03 20:05:52.720: WARN/System.err(279):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-03 20:05:52.729: WARN/System.err(279):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-03 20:05:52.729: WARN/System.err(279):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-03 20:05:52.750: WARN/System.err(279):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 20:05:52.750: WARN/System.err(279):     at android.os.Looper.loop(Looper.java:123)
02-03 20:05:52.760: WARN/System.err(279):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-03 20:05:52.771: WARN/System.err(279):     at java.lang.reflect.Method.invokeNative(Native Method)
02-03 20:05:52.781: WARN/System.err(279):     at java.lang.reflect.Method.invoke(Method.java:521)
02-03 20:05:52.781: WARN/System.err(279):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-03 20:05:52.799: WARN/System.err(279):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-03 20:05:52.799: WARN/System.err(279):     at dalvik.system.NativeStart.main(Native Method)
02-03 20:05:57.439: DEBUG/KeyguardViewMediator(59): pokeWakelock(5000)
02-03 20:05:57.630: DEBUG/KeyguardViewMediator(59): pokeWakelock(5000)
02-03 20:05:57.759: INFO/ActivityManager(59): Displayed activity cpe495.smartapp/.SmartApp: 10694 ms (total 10694 ms)
02-03 20:05:57.940: INFO/ARMAssembler(59): generated scanline__00000077:03545404_00000004_00000000 [ 47 ipp] (67 ins) at [0x2bd108:0x2bd214] in 2142171 ns
02-03 20:05:58.029: INFO/ARMAssembler(59): generated scanline__00000177:03515104_00001001_00000000 [ 91 ipp] (114 ins) at [0x2bd218:0x2bd3e0] in 1052927 ns   

用户使用首选项菜单更改首选项中的 HeartRateBase 值后,会打印出下面的日志猫。它似乎发生在 onpreferenceschanged 函数内部。

02-03 20:06:19.000: VERBOSE/onsharedpreferencechanged(279): inside
02-03 20:06:19.009: WARN/System.err(279): java.lang.ClassCastException: java.lang.String
02-03 20:06:19.019: WARN/System.err(279):     at android.app.ContextImpl$SharedPreferencesImpl.getInt(ContextImpl.java:2706)
02-03 20:06:19.031: WARN/System.err(279):     at cpe495.smartapp.SmartApp.onSharedPreferenceChanged(SmartApp.java:131)
02-03 20:06:19.040: WARN/System.err(279):     at android.app.ContextImpl$SharedPreferencesImpl$EditorImpl.commit(ContextImpl.java:2830)
02-03 20:06:19.040: WARN/System.err(279):     at android.preference.Preference.tryCommit(Preference.java:1198)
02-03 20:06:19.050: WARN/System.err(279):     at android.preference.Preference.persistString(Preference.java:1225)
02-03 20:06:19.060: WARN/System.err(279):     at android.preference.ListPreference.setValue(ListPreference.java:126)
02-03 20:06:19.070: WARN/System.err(279):     at android.preference.ListPreference.onDialogClosed(ListPreference.java:220)
02-03 20:06:19.120: WARN/System.err(279):     at android.preference.DialogPreference.onDismiss(DialogPreference.java:384)
02-03 20:06:19.139: WARN/System.err(279):     at android.app.Dialog$ListenersHandler.handleMessage(Dialog.java:1047)
02-03 20:06:19.149: WARN/System.err(279):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 20:06:19.149: WARN/System.err(279):     at android.os.Looper.loop(Looper.java:123)
02-03 20:06:19.170: WARN/System.err(279):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-03 20:06:19.170: WARN/System.err(279):     at java.lang.reflect.Method.invokeNative(Native Method)
02-03 20:06:19.180: WARN/System.err(279):     at java.lang.reflect.Method.invoke(Method.java:521)
02-03 20:06:19.190: WARN/System.err(279):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-03 20:06:19.200: WARN/System.err(279):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-03 20:06:19.200: WARN/System.err(279):     at dalvik.system.NativeStart.main(Native Method)
02-03 20:06:19.320: WARN/InputManagerService(59): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@43e60f18   

从这里开始是我解决这些问题的代码。


//The Main Class, mainly my gui class
public class SmartApp extends Activity implements OnSharedPreferenceChangeListener {

    private DataRobot dR = new DataRobot();
    public SmartApp() {}
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.intro);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        PreferenceManager.setDefaultValues(this, R.xml.settings, false);
        prefs.registerOnSharedPreferenceChangeListener(this);

        //set remembered preferences
        try {
            dR.sethRB(prefs.getInt("heartRateBaseKey", 60));
            dR.sethRVMax(prefs.getInt("hRVMaxKey", 100));
            dR.sethRVMin(prefs.getInt("hRVMinKey", 0));
            dR.setsI1(prefs.getInt("sI1Key", 50));
            dR.setsI2(prefs.getInt("sI2Key", 80));
            dR.setW1(prefs.getInt("weight1Key", 20));
            dR.setW2(prefs.getInt("weight2Key", 40));
            dR.setW3(prefs.getInt("weight3Key", 60));
        }
        catch (ClassCastException e) {
            e.printStackTrace();
        }

        final Button smartConnectionSettingsButton = (Button) findViewById(R.id.smartConnectionSettingsButton);
        smartConnectionSettingsButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent settingsIntent = new Intent(v.getContext(), Settings.class);
                        startActivityForResult(settingsIntent, 0);
                    }
                });
    }
    @Override
    public void onSharedPreferenceChanged(SharedPreferences prefs,
            String key) {
        // TODO Auto-generated method stub
        Log.v("onsharedpreferencechanged", "inside");
        try {
            if(key.equals("heartRateBaseKey")) {
                dR.sethRB(prefs.getInt("heartRateBaseKey", 60));
            }
            else if(key.equals("hRVMaxKey")) {
                dR.sethRVMax(prefs.getInt("hRVMaxKey", 100));
            }
            else if(key.equals("hRVMinKey")) {
                dR.sethRVMin(prefs.getInt("hRVMinKey", 0));
            }
            else if(key.equals("sI1Key")) {
                dR.setsI1(prefs.getInt("sI1Key", 50));
            }
            else if(key.equals("sI2Key")) {
                dR.setsI2(prefs.getInt("sI2Key", 80));
            }
            else if(key.equals("weight1Key")) {
                dR.setW1(prefs.getInt("weight1Key", 20));
            }
            else if(key.equals("weight2Key")) {
                dR.setW2(prefs.getInt("weight2Key", 40));
            }
            else if(key.equals("weight3Key")) {
                dR.setW3(prefs.getInt("weight3Key", 60));
            }
        }
        catch (ClassCastException e) {
            e.printStackTrace();
        }

    }
}    

这个类是我分析数据的地方,我需要在应用程序启动时设置这里的私有值。目前我正在 SmartApp 的 onCreate 方法中执行此操作。如果用户在运行时更改任何首选项,我还需要实时更改值。

public class DataRobot {
    /* This class is for analyzing the data */
    private SmartDataObject data;

    private int sI1;
    private int sI2;
    private int w1;
    private int w2;
    private int w3;
    private int hRB;
    private int hRVMin;
    private int hRVMax;
    public boolean analyzeData(SmartDataObject temp) {}

    public void setsI1(int temp) {
        sI1 = temp;
    }
    public void setsI2(int temp) {
        sI2 = temp;
    }
    public void setW1(int temp) {
        w1 = temp;
    }
    public void setW2(int temp) {
        w2 = temp;
    }
    public void setW3(int temp) {
        w3 = temp;
    }
    public void sethRB(int temp) {
        hRB = temp;
    }
    public void sethRVMin(int temp) {
        hRVMin = temp;
    }
    public void sethRVMax(int temp) {
        hRVMax = temp;
    }
}   

下面是首选项类

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class Preferences extends PreferenceActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);
    }
}

下面是我的首选项的 XML 文件,名为“settings.xml”。

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
  xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Profile Settings">
<EditTextPreference 
android:title="First Name" 
android:key="firstNameKey" 
android:selectable="false"></EditTextPreference>
<EditTextPreference 
android:title="Last Name" 
android:key="lastNameKey" 
android:selectable="false"></EditTextPreference>
<EditTextPreference 
android:title="User Name" 
android:key="userNameKey" 
android:selectable="false"></EditTextPreference>
<EditTextPreference 
android:title="Birth Date" 
android:key="birthDateKey" 
android:selectable="false"></EditTextPreference>
</PreferenceCategory>
<PreferenceCategory 
android:title="Configuration Settings">
<ListPreference 
android:title="Medium Stress Index Threshold" 
android:entryValues="@array/sI1Values" 
android:entries="@array/sI1Array" 
android:key="sI1Key"></ListPreference>
<ListPreference 
android:title="High Stress Index Threshold" 
android:entryValues="@array/sI1Values" 
android:entries="@array/sI1Array" 
android:key="sI2Key"></ListPreference>
<EditTextPreference 
android:title="Weight 1" 
android:key="weight1Key"></EditTextPreference>
<EditTextPreference 
android:title="Weight 2" 
android:key="weight2Key"></EditTextPreference>
<EditTextPreference 
android:title="Weight 3" 
android:key="weight3Key"></EditTextPreference>    
<ListPreference
android:entryValues="@array/heartRateBaseValues" 
android:entries="@array/heartRateBaseArray" 
android:defaultValue="60" 
android:key="heartRateBaseKey" 
android:title="Heart Rate Base"></ListPreference>
<EditTextPreference 
android:title="Heart Rate Variability Minimum" 
android:key="hRVMinKey"></EditTextPreference>
<EditTextPreference 
android:title="Heart Rate Variability Maximum" 
android:key="hRVMaxKey"></EditTextPreference>    
</PreferenceCategory>
</PreferenceScreen>   

下面是“arrayValues.xml”文件,其中包含某些内容的一些值。

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="heartRateBaseArray">
<item>10 bpm</item>
<item>20 bpm</item>
<item>30 bpm</item>
<item>40 bpm</item>
<item>50 bpm</item>
<item>60 bpm</item>
<item>70 bpm</item>
<item>80 bpm</item>
<item>90 bpm</item>
<item>100 bpm</item>
</string-array>
<string-array name="heartRateBaseValues">
<item>10</item>
<item>20</item>
<item>30</item>
<item>40</item>
<item>50</item>
<item>60</item>
<item>70</item>
<item>80</item>
<item>90</item>
<item>100</item>
</string-array>
<string-array name="sI1Array">
<item>0</item>
<item>5</item>
<item>10</item>
<item>15</item>
<item>20</item>
<item>25</item>
<item>30</item>
<item>35</item>
<item>40</item>
<item>45</item>
<item>50</item>
<item>55</item>
<item>60</item>
<item>65</item>
<item>70</item>
<item>75</item>
<item>80</item>
<item>85</item>
<item>90</item>
<item>95</item>
<item>100</item>
</string-array>
<string-array name="sI1Values">
<item>0</item>
<item>5</item>
<item>10</item>
<item>15</item>
<item>20</item>
<item>25</item>
<item>30</item>
<item>35</item>
<item>40</item>
<item>45</item>
<item>50</item>
<item>55</item>
<item>60</item>
<item>65</item>
<item>70</item>
<item>75</item>
<item>80</item>
<item>85</item>
<item>90</item>
<item>95</item>
<item>100</item>
</string-array>
</resources>   

Another question about my implementation. I am fairly new to java/android coming from a c/c++ back ground. I already have this implemented in my code, i am just having a couple issues with it and was also wondering if this was the best solution and if anyone had any suggestions or could help me with my problem. Here is the problem and my questions:

My android application needs to have
the capabilities of loading settings
from an xml file and the application
needs to allow the user to change
those settings and remember the
changed settings. I also need my
application to reference those changes
and detect when the user has changed
one of the settings.

I currently have the above working, except that i want the default settings loaded from my xml file instead of the method used in my code (view the code to see what im talking about). I have searched around for this and can not seem to find a solution.

Once a change is detected i need it to
load the value into my DataRobot class
variables. I have accessor functions
for this (see code). However, when i
attempt to change any of the settings
it does not change the value in my
DataRobot class.

Those two block quotes are explaining what i need, basically. I am using android preferences to solve this problem currently. Would this be the best way to solve this problem?

If you need any additional information please let me know. Thanks for your assistance in advance, it is greatly appreciated!


Also, i am currently experiencing a few errors with the current setup, listed below is an explanation of when the error is occurring and the log cat of said error.

The log cat output below happens when the application is first launching. It appears that it is having trouble converting a string and threw a ClassCastException. I am not sure where or why this is occurring. All the code is at the bottom of the post.

02-03 20:05:50.731: INFO/System.out(279): debugger has settled (1384)
02-03 20:05:52.080: INFO/ActivityManager(59): Displayed activity com.android.launcher/com.android.launcher2.Launcher: 38755 ms (total 38755 ms)
02-03 20:05:52.480: WARN/Resources(279): Converting to string: TypedValue{t=0x10/d=0x3c a=-1}
02-03 20:05:52.669: WARN/System.err(279): java.lang.ClassCastException: java.lang.String
02-03 20:05:52.689: WARN/System.err(279):     at android.app.ContextImpl$SharedPreferencesImpl.getInt(ContextImpl.java:2706)
02-03 20:05:52.689: WARN/System.err(279):     at cpe495.smartapp.SmartApp.onCreate(SmartApp.java:61)
02-03 20:05:52.701: WARN/System.err(279):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-03 20:05:52.715: WARN/System.err(279):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-03 20:05:52.720: WARN/System.err(279):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-03 20:05:52.729: WARN/System.err(279):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-03 20:05:52.729: WARN/System.err(279):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-03 20:05:52.750: WARN/System.err(279):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 20:05:52.750: WARN/System.err(279):     at android.os.Looper.loop(Looper.java:123)
02-03 20:05:52.760: WARN/System.err(279):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-03 20:05:52.771: WARN/System.err(279):     at java.lang.reflect.Method.invokeNative(Native Method)
02-03 20:05:52.781: WARN/System.err(279):     at java.lang.reflect.Method.invoke(Method.java:521)
02-03 20:05:52.781: WARN/System.err(279):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-03 20:05:52.799: WARN/System.err(279):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-03 20:05:52.799: WARN/System.err(279):     at dalvik.system.NativeStart.main(Native Method)
02-03 20:05:57.439: DEBUG/KeyguardViewMediator(59): pokeWakelock(5000)
02-03 20:05:57.630: DEBUG/KeyguardViewMediator(59): pokeWakelock(5000)
02-03 20:05:57.759: INFO/ActivityManager(59): Displayed activity cpe495.smartapp/.SmartApp: 10694 ms (total 10694 ms)
02-03 20:05:57.940: INFO/ARMAssembler(59): generated scanline__00000077:03545404_00000004_00000000 [ 47 ipp] (67 ins) at [0x2bd108:0x2bd214] in 2142171 ns
02-03 20:05:58.029: INFO/ARMAssembler(59): generated scanline__00000177:03515104_00001001_00000000 [ 91 ipp] (114 ins) at [0x2bd218:0x2bd3e0] in 1052927 ns   

The log cat below is printed out after the user uses the preferences menu to change the HeartRateBase value in the preferences. It appears to be happening inside the onpreferenceschanged function.

02-03 20:06:19.000: VERBOSE/onsharedpreferencechanged(279): inside
02-03 20:06:19.009: WARN/System.err(279): java.lang.ClassCastException: java.lang.String
02-03 20:06:19.019: WARN/System.err(279):     at android.app.ContextImpl$SharedPreferencesImpl.getInt(ContextImpl.java:2706)
02-03 20:06:19.031: WARN/System.err(279):     at cpe495.smartapp.SmartApp.onSharedPreferenceChanged(SmartApp.java:131)
02-03 20:06:19.040: WARN/System.err(279):     at android.app.ContextImpl$SharedPreferencesImpl$EditorImpl.commit(ContextImpl.java:2830)
02-03 20:06:19.040: WARN/System.err(279):     at android.preference.Preference.tryCommit(Preference.java:1198)
02-03 20:06:19.050: WARN/System.err(279):     at android.preference.Preference.persistString(Preference.java:1225)
02-03 20:06:19.060: WARN/System.err(279):     at android.preference.ListPreference.setValue(ListPreference.java:126)
02-03 20:06:19.070: WARN/System.err(279):     at android.preference.ListPreference.onDialogClosed(ListPreference.java:220)
02-03 20:06:19.120: WARN/System.err(279):     at android.preference.DialogPreference.onDismiss(DialogPreference.java:384)
02-03 20:06:19.139: WARN/System.err(279):     at android.app.Dialog$ListenersHandler.handleMessage(Dialog.java:1047)
02-03 20:06:19.149: WARN/System.err(279):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 20:06:19.149: WARN/System.err(279):     at android.os.Looper.loop(Looper.java:123)
02-03 20:06:19.170: WARN/System.err(279):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-03 20:06:19.170: WARN/System.err(279):     at java.lang.reflect.Method.invokeNative(Native Method)
02-03 20:06:19.180: WARN/System.err(279):     at java.lang.reflect.Method.invoke(Method.java:521)
02-03 20:06:19.190: WARN/System.err(279):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-03 20:06:19.200: WARN/System.err(279):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-03 20:06:19.200: WARN/System.err(279):     at dalvik.system.NativeStart.main(Native Method)
02-03 20:06:19.320: WARN/InputManagerService(59): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@43e60f18   

Starting here is the code that i have for these issues.


//The Main Class, mainly my gui class
public class SmartApp extends Activity implements OnSharedPreferenceChangeListener {

    private DataRobot dR = new DataRobot();
    public SmartApp() {}
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.intro);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        PreferenceManager.setDefaultValues(this, R.xml.settings, false);
        prefs.registerOnSharedPreferenceChangeListener(this);

        //set remembered preferences
        try {
            dR.sethRB(prefs.getInt("heartRateBaseKey", 60));
            dR.sethRVMax(prefs.getInt("hRVMaxKey", 100));
            dR.sethRVMin(prefs.getInt("hRVMinKey", 0));
            dR.setsI1(prefs.getInt("sI1Key", 50));
            dR.setsI2(prefs.getInt("sI2Key", 80));
            dR.setW1(prefs.getInt("weight1Key", 20));
            dR.setW2(prefs.getInt("weight2Key", 40));
            dR.setW3(prefs.getInt("weight3Key", 60));
        }
        catch (ClassCastException e) {
            e.printStackTrace();
        }

        final Button smartConnectionSettingsButton = (Button) findViewById(R.id.smartConnectionSettingsButton);
        smartConnectionSettingsButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent settingsIntent = new Intent(v.getContext(), Settings.class);
                        startActivityForResult(settingsIntent, 0);
                    }
                });
    }
    @Override
    public void onSharedPreferenceChanged(SharedPreferences prefs,
            String key) {
        // TODO Auto-generated method stub
        Log.v("onsharedpreferencechanged", "inside");
        try {
            if(key.equals("heartRateBaseKey")) {
                dR.sethRB(prefs.getInt("heartRateBaseKey", 60));
            }
            else if(key.equals("hRVMaxKey")) {
                dR.sethRVMax(prefs.getInt("hRVMaxKey", 100));
            }
            else if(key.equals("hRVMinKey")) {
                dR.sethRVMin(prefs.getInt("hRVMinKey", 0));
            }
            else if(key.equals("sI1Key")) {
                dR.setsI1(prefs.getInt("sI1Key", 50));
            }
            else if(key.equals("sI2Key")) {
                dR.setsI2(prefs.getInt("sI2Key", 80));
            }
            else if(key.equals("weight1Key")) {
                dR.setW1(prefs.getInt("weight1Key", 20));
            }
            else if(key.equals("weight2Key")) {
                dR.setW2(prefs.getInt("weight2Key", 40));
            }
            else if(key.equals("weight3Key")) {
                dR.setW3(prefs.getInt("weight3Key", 60));
            }
        }
        catch (ClassCastException e) {
            e.printStackTrace();
        }

    }
}    

This class is where i am analyzing the data and i need the privates here to be set at application launch time. Currently i am doing that in the onCreate method in SmartApp. I also need the values changed in real time if a user changes any of the preferences during run time.

public class DataRobot {
    /* This class is for analyzing the data */
    private SmartDataObject data;

    private int sI1;
    private int sI2;
    private int w1;
    private int w2;
    private int w3;
    private int hRB;
    private int hRVMin;
    private int hRVMax;
    public boolean analyzeData(SmartDataObject temp) {}

    public void setsI1(int temp) {
        sI1 = temp;
    }
    public void setsI2(int temp) {
        sI2 = temp;
    }
    public void setW1(int temp) {
        w1 = temp;
    }
    public void setW2(int temp) {
        w2 = temp;
    }
    public void setW3(int temp) {
        w3 = temp;
    }
    public void sethRB(int temp) {
        hRB = temp;
    }
    public void sethRVMin(int temp) {
        hRVMin = temp;
    }
    public void sethRVMax(int temp) {
        hRVMax = temp;
    }
}   

Below is the preferences class

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class Preferences extends PreferenceActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);
    }
}

Below is the XML file for my preferences named "settings.xml".

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
  xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Profile Settings">
<EditTextPreference 
android:title="First Name" 
android:key="firstNameKey" 
android:selectable="false"></EditTextPreference>
<EditTextPreference 
android:title="Last Name" 
android:key="lastNameKey" 
android:selectable="false"></EditTextPreference>
<EditTextPreference 
android:title="User Name" 
android:key="userNameKey" 
android:selectable="false"></EditTextPreference>
<EditTextPreference 
android:title="Birth Date" 
android:key="birthDateKey" 
android:selectable="false"></EditTextPreference>
</PreferenceCategory>
<PreferenceCategory 
android:title="Configuration Settings">
<ListPreference 
android:title="Medium Stress Index Threshold" 
android:entryValues="@array/sI1Values" 
android:entries="@array/sI1Array" 
android:key="sI1Key"></ListPreference>
<ListPreference 
android:title="High Stress Index Threshold" 
android:entryValues="@array/sI1Values" 
android:entries="@array/sI1Array" 
android:key="sI2Key"></ListPreference>
<EditTextPreference 
android:title="Weight 1" 
android:key="weight1Key"></EditTextPreference>
<EditTextPreference 
android:title="Weight 2" 
android:key="weight2Key"></EditTextPreference>
<EditTextPreference 
android:title="Weight 3" 
android:key="weight3Key"></EditTextPreference>    
<ListPreference
android:entryValues="@array/heartRateBaseValues" 
android:entries="@array/heartRateBaseArray" 
android:defaultValue="60" 
android:key="heartRateBaseKey" 
android:title="Heart Rate Base"></ListPreference>
<EditTextPreference 
android:title="Heart Rate Variability Minimum" 
android:key="hRVMinKey"></EditTextPreference>
<EditTextPreference 
android:title="Heart Rate Variability Maximum" 
android:key="hRVMaxKey"></EditTextPreference>    
</PreferenceCategory>
</PreferenceScreen>   

Below is the "arrayValues.xml" file that holds some values for some stuff.

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="heartRateBaseArray">
<item>10 bpm</item>
<item>20 bpm</item>
<item>30 bpm</item>
<item>40 bpm</item>
<item>50 bpm</item>
<item>60 bpm</item>
<item>70 bpm</item>
<item>80 bpm</item>
<item>90 bpm</item>
<item>100 bpm</item>
</string-array>
<string-array name="heartRateBaseValues">
<item>10</item>
<item>20</item>
<item>30</item>
<item>40</item>
<item>50</item>
<item>60</item>
<item>70</item>
<item>80</item>
<item>90</item>
<item>100</item>
</string-array>
<string-array name="sI1Array">
<item>0</item>
<item>5</item>
<item>10</item>
<item>15</item>
<item>20</item>
<item>25</item>
<item>30</item>
<item>35</item>
<item>40</item>
<item>45</item>
<item>50</item>
<item>55</item>
<item>60</item>
<item>65</item>
<item>70</item>
<item>75</item>
<item>80</item>
<item>85</item>
<item>90</item>
<item>95</item>
<item>100</item>
</string-array>
<string-array name="sI1Values">
<item>0</item>
<item>5</item>
<item>10</item>
<item>15</item>
<item>20</item>
<item>25</item>
<item>30</item>
<item>35</item>
<item>40</item>
<item>45</item>
<item>50</item>
<item>55</item>
<item>60</item>
<item>65</item>
<item>70</item>
<item>75</item>
<item>80</item>
<item>85</item>
<item>90</item>
<item>95</item>
<item>100</item>
</string-array>
</resources>   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

客…行舟 2024-10-22 19:53:18

无需深入研究您的代码。我首先查看 SmartApp 代码中的第 61 行和第 131 行。这是两种情况下异常开始的地方。如果在这两种情况下这都指向使用 prefs.getInt() 的调用,我猜它与您请求 int 有关,而实际上原始 XML 条目是字符串。

Without going into great depth in your code. I'd start by looking at lines 61 and 131 in your SmartApp code. This is where the exception is starting in both cases. If in both cases this points to a call using prefs.getInt(), I'd guess that it has something to do with you asking for a int when in fact the original XML entries are Strings.

浅唱々樱花落 2024-10-22 19:53:18

对于您的错误,我的应用程序也遇到了类似的问题,解决此问题的方法是将首选项作为 String 对象检索,然后使用 Integer.parse() 将 String 转换为数字。这有点麻烦,但它为我完成了工作。

至于您最初的问题,当发生更改时,您是否可以让您的 PreferenceActivity 写入某个特定文件,以便其他活动可以定期检查或检查其创建并相应地调整/提取信息?

For your error, I have had a similar problem with my applications and the way I got around this was retrieving the preferences as String objects and then using Integer.parse() to convert the String to a number. It's a bit of a hack but it got the job done for me.

As for your initial question could you maybe have your PreferenceActivity write to a certain specific file when a change is made that the other Activity can check periodically or on it's creation and adjust/pull information accordingly?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文