android首选项导致错误

发布于 2024-10-15 18:17:58 字数 11148 浏览 6 评论 0原文

我正在尝试在我的应用程序中添加一些首选项功能。我希望应用程序能够加载 XML 文件中设置的默认值,然后能够在以后更改这些值。我遇到了首选项并一直尝试在我的代码中实现。我计算出了下面的代码,并在我的 SmartApp< 中尝试运行 "//STARTING HERE""//ENDING HERE" 注释之间的代码时/strong> 类它不会启动我的 gui(屏幕是空白的并且似乎没有执行任何操作)并给出以下 logcat 消息。当我在注释掉该区域的情况下运行时,应用程序运行正常。有人可以看看我有什么,看看我可能做错了什么,或者对我可能需要解决这个问题有什么建议吗?如果您有任何疑问或需要更多信息,请告诉我。 提前致谢,如果我找到解决方案,我会将其发布在这里。


//The log cat messages
02-01 22:49:19.653: INFO/ActivityManager(59): Displayed activity com.android.launcher/com.android.launcher2.Launcher: 43150 ms (total 43150 ms)
02-01 22:49:24.513: WARN/ActivityManager(59): Launch timeout has expired, giving up wake lock!
02-01 22:49:25.560: WARN/ActivityManager(59): Activity idle timeout for HistoryRecord{43f017d0 cpe495.smartapp/.SmartApp}
02-01 22:49:29.393: DEBUG/KeyguardViewMediator(59): pokeWakelock(5000)
02-01 22:49:29.613: DEBUG/KeyguardViewMediator(59): pokeWakelock(5000)
02-01 22:49:29.862: INFO/ARMAssembler(59): generated scanline__00000077:03545404_00000004_00000000 [ 47 ipp] (67 ins) at [0x337450:0x33755c] in 1480914 ns
02-01 22:49:29.952: INFO/ARMAssembler(59): generated scanline__00000177:03515104_00001001_00000000 [ 91 ipp] (114 ins) at [0x337560:0x337728] in 1085613 ns
02-01 22:53:59.112: DEBUG/SntpClient(59): request time failed: java.net.SocketException: Address family not supported by protocol    

//The Array Values
<?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>  

//The settings.xml file
<?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>   

//The preferences class
package cpe495.smartapp;

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

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

//The Main Class
public class SmartApp extends Activity implements OnSharedPreferenceChangeListener {
    TextView smartConnectionStatus;
    TextView testOutputView;
    Thread cThread;
    private ConnectDevice cD = new ConnectDevice();
    private DataRobot dR = new DataRobot();
    private DataBuilder dB = new DataBuilder();
    private DataSender dS = new DataSender();
    public SmartApp() {
        /* Constructor */
        cD.addDataReceivedListener(new DataReceivedListener() {
            @Override
            public void dataReceivedReceived(DataReceivedEvent event) {
                // TODO Auto-generated method stub
                dR.analyzeData(event.getData());
            }
        });
        dR.addDataAnalyzedListener(new DataAnalyzedListener() {
            @Override
            public void dataAnalyzedReceived(DataAnalyzedEvent event) {
                // TODO Auto-generated method stub
                dB.submitData(event.getData());
            }
        });
        dB.addDataBuilderListener(new DataBuilderListener() {
            @Override
            public void dataBuilderReceived(DataBuilderEvent event) {
                // TODO Auto-generated method stub
                dS.sendData(event.getData());
            }
        });
    }
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.intro);

        //STARTING HERE
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

        prefs.registerOnSharedPreferenceChangeListener(this);

        //set remembered preferences
        dR.sethRB(Integer.parseInt((prefs.getString("heartRateBaseKey", null))));
        dR.sethRVMax(Integer.parseInt((prefs.getString("hRVMaxKey", null))));
        dR.sethRVMin(Integer.parseInt((prefs.getString("hRVMinKey", null))));
        dR.setsI1(Integer.parseInt((prefs.getString("sI1Key", null))));
        dR.setsI2(Integer.parseInt((prefs.getString("sI2Key", null))));
        dR.setW1(Integer.parseInt((prefs.getString("weight1Key", null))));
        dR.setW2(Integer.parseInt((prefs.getString("weight2Key", null))));
        dR.setW3(Integer.parseInt((prefs.getString("weight3Key", null))));
        //ENDING HERE            


        smartConnectionStatus = (TextView) findViewById(R.id.smartConnectionStatus);
        testOutputView = (TextView) findViewById(R.id.testingOutput);

        final Button firstTimeButton = (Button) findViewById(R.id.firstTimeButton);
        firstTimeButton.setOnClickListener(
        new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent userCreationIntent = new Intent(v.getContext(), UserCreation.class);
                startActivityForResult(userCreationIntent, 0);

            }
        });

        final Button connectDeviceButton = (Button) findViewById(R.id.connectDeviceButton);
        connectDeviceButton.setOnClickListener(
        new View.OnClickListener() {    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //Intent connectDeviceIntent = new Intent(v.getContext(), ConnectDevice.class);
                //startActivityForResult(connectDeviceIntent, 0);

                cThread = new Thread(cD);
                cThread.start();
            }
        });

        final Button disconnectDeviceButton = (Button) findViewById(R.id.disconnectDeviceButton);
        disconnectDeviceButton.setOnClickListener(
        new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                cD.setConnected(false);
            }
        });
    }
    @Override
    public void onSharedPreferenceChanged(SharedPreferences prefs,
            String key) {
        // TODO Auto-generated method stub
        if(key.equals("heartRateBaseKey")) {
            dR.sethRB(Integer.parseInt((prefs.getString("heartRateBaseKey", null))));
        }
        else if(key.equals("hRVMaxKey")) {
            dR.sethRVMax(Integer.parseInt((prefs.getString("hRVMaxKey", null))));
        }
        else if(key.equals("hRVMinKey")) {
            dR.sethRVMin(Integer.parseInt((prefs.getString("hRVMinKey", null))));
        }
        else if(key.equals("sI1Key")) {
            dR.setsI1(Integer.parseInt((prefs.getString("sI1Key", null))));
        }
        else if(key.equals("sI2Key")) {
            dR.setsI2(Integer.parseInt((prefs.getString("sI2Key", null))));
        }
        else if(key.equals("weight1Key")) {
            dR.setW1(Integer.parseInt((prefs.getString("weight1Key", null))));
        }
        else if(key.equals("weight2Key")) {
            dR.setW2(Integer.parseInt((prefs.getString("weight2Key", null))));
        }
        else if(key.equals("weight3Key")) {
            dR.setW3(Integer.parseInt((prefs.getString("weight3Key", null))));
        }
    }
}

I am trying to add some preferences abilities into my application. I want the application to be able to load default values that are set in an XML file and then be able to change those values later. I ran across Preferences and have been trying to implement in my code. I worked out the code below and when trying to run the code between the "//STARTING HERE" and "//ENDING HERE" comments in my SmartApp class it does not launch my gui (the screen is blank and does not appear to be doing anything) and gives the following logcat messages. When i run with that area commented out, the application runs like normal. Can someone please take a look at what i have and see what i might be doing wrong or have suggestions for what i might need to fix this issue? If you have any questions or need more information, please let me know. Thanks in advance, if i find the solution i will post it here.


//The log cat messages
02-01 22:49:19.653: INFO/ActivityManager(59): Displayed activity com.android.launcher/com.android.launcher2.Launcher: 43150 ms (total 43150 ms)
02-01 22:49:24.513: WARN/ActivityManager(59): Launch timeout has expired, giving up wake lock!
02-01 22:49:25.560: WARN/ActivityManager(59): Activity idle timeout for HistoryRecord{43f017d0 cpe495.smartapp/.SmartApp}
02-01 22:49:29.393: DEBUG/KeyguardViewMediator(59): pokeWakelock(5000)
02-01 22:49:29.613: DEBUG/KeyguardViewMediator(59): pokeWakelock(5000)
02-01 22:49:29.862: INFO/ARMAssembler(59): generated scanline__00000077:03545404_00000004_00000000 [ 47 ipp] (67 ins) at [0x337450:0x33755c] in 1480914 ns
02-01 22:49:29.952: INFO/ARMAssembler(59): generated scanline__00000177:03515104_00001001_00000000 [ 91 ipp] (114 ins) at [0x337560:0x337728] in 1085613 ns
02-01 22:53:59.112: DEBUG/SntpClient(59): request time failed: java.net.SocketException: Address family not supported by protocol    

//The Array Values
<?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>  

//The settings.xml file
<?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>   

//The preferences class
package cpe495.smartapp;

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

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

//The Main Class
public class SmartApp extends Activity implements OnSharedPreferenceChangeListener {
    TextView smartConnectionStatus;
    TextView testOutputView;
    Thread cThread;
    private ConnectDevice cD = new ConnectDevice();
    private DataRobot dR = new DataRobot();
    private DataBuilder dB = new DataBuilder();
    private DataSender dS = new DataSender();
    public SmartApp() {
        /* Constructor */
        cD.addDataReceivedListener(new DataReceivedListener() {
            @Override
            public void dataReceivedReceived(DataReceivedEvent event) {
                // TODO Auto-generated method stub
                dR.analyzeData(event.getData());
            }
        });
        dR.addDataAnalyzedListener(new DataAnalyzedListener() {
            @Override
            public void dataAnalyzedReceived(DataAnalyzedEvent event) {
                // TODO Auto-generated method stub
                dB.submitData(event.getData());
            }
        });
        dB.addDataBuilderListener(new DataBuilderListener() {
            @Override
            public void dataBuilderReceived(DataBuilderEvent event) {
                // TODO Auto-generated method stub
                dS.sendData(event.getData());
            }
        });
    }
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.intro);

        //STARTING HERE
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

        prefs.registerOnSharedPreferenceChangeListener(this);

        //set remembered preferences
        dR.sethRB(Integer.parseInt((prefs.getString("heartRateBaseKey", null))));
        dR.sethRVMax(Integer.parseInt((prefs.getString("hRVMaxKey", null))));
        dR.sethRVMin(Integer.parseInt((prefs.getString("hRVMinKey", null))));
        dR.setsI1(Integer.parseInt((prefs.getString("sI1Key", null))));
        dR.setsI2(Integer.parseInt((prefs.getString("sI2Key", null))));
        dR.setW1(Integer.parseInt((prefs.getString("weight1Key", null))));
        dR.setW2(Integer.parseInt((prefs.getString("weight2Key", null))));
        dR.setW3(Integer.parseInt((prefs.getString("weight3Key", null))));
        //ENDING HERE            


        smartConnectionStatus = (TextView) findViewById(R.id.smartConnectionStatus);
        testOutputView = (TextView) findViewById(R.id.testingOutput);

        final Button firstTimeButton = (Button) findViewById(R.id.firstTimeButton);
        firstTimeButton.setOnClickListener(
        new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent userCreationIntent = new Intent(v.getContext(), UserCreation.class);
                startActivityForResult(userCreationIntent, 0);

            }
        });

        final Button connectDeviceButton = (Button) findViewById(R.id.connectDeviceButton);
        connectDeviceButton.setOnClickListener(
        new View.OnClickListener() {    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //Intent connectDeviceIntent = new Intent(v.getContext(), ConnectDevice.class);
                //startActivityForResult(connectDeviceIntent, 0);

                cThread = new Thread(cD);
                cThread.start();
            }
        });

        final Button disconnectDeviceButton = (Button) findViewById(R.id.disconnectDeviceButton);
        disconnectDeviceButton.setOnClickListener(
        new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                cD.setConnected(false);
            }
        });
    }
    @Override
    public void onSharedPreferenceChanged(SharedPreferences prefs,
            String key) {
        // TODO Auto-generated method stub
        if(key.equals("heartRateBaseKey")) {
            dR.sethRB(Integer.parseInt((prefs.getString("heartRateBaseKey", null))));
        }
        else if(key.equals("hRVMaxKey")) {
            dR.sethRVMax(Integer.parseInt((prefs.getString("hRVMaxKey", null))));
        }
        else if(key.equals("hRVMinKey")) {
            dR.sethRVMin(Integer.parseInt((prefs.getString("hRVMinKey", null))));
        }
        else if(key.equals("sI1Key")) {
            dR.setsI1(Integer.parseInt((prefs.getString("sI1Key", null))));
        }
        else if(key.equals("sI2Key")) {
            dR.setsI2(Integer.parseInt((prefs.getString("sI2Key", null))));
        }
        else if(key.equals("weight1Key")) {
            dR.setW1(Integer.parseInt((prefs.getString("weight1Key", null))));
        }
        else if(key.equals("weight2Key")) {
            dR.setW2(Integer.parseInt((prefs.getString("weight2Key", null))));
        }
        else if(key.equals("weight3Key")) {
            dR.setW3(Integer.parseInt((prefs.getString("weight3Key", null))));
        }
    }
}

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

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

发布评论

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

评论(1

带刺的爱情 2024-10-22 18:17:58

我认为这段代码有几个问题。首先,从您的首选项加载时,您拥有的默认值全部为 nullInteger#parseInt 的文档() 声明,如果您传入 null,它将抛出 NumberFormatException。请改用 0 或其他值。

第二个是我认为您对 dR 方法的所有调用都需要很长时间才能完成。这些功能有什么作用?我敢打赌,它们是一些密集型任务,并且由于它们位于您的 onCreate() 方法中,操作系统会认为您的应用程序启动时间太长,从而杀死它。将这些调用移出到 AsyncTask 中,以便可以在后台加载它们。

I think there are a couple of issue with this code. The first is that the default values you have are all null when loading from your preferences. The documentation for Integer#parseInt() states that it will throw a NumberFormatException if you pass in null. Use 0 or some other value instead.

The second is that I think all your calls to the dR methods are taking a long time to complete. What do these functions do? I'm betting that they are somewhat intensive tasks, and since they are in your onCreate() method the operating system thinks that your app has taken too long to start and thus kills it. Move these calls out into an AsyncTask so that they can be loaded in the background.

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