gpt4 book ai didi

java - 导致错误的android偏好设置

转载 作者:行者123 更新时间:2023-11-29 22:29:11 24 4
gpt4 key购买 nike

我想在我的应用程序中添加一些偏好功能。我希望应用程序能够加载在 XML 文件中设置的默认值,然后能够在以后更改这些值。我遇到了 Preferences 并且一直在尝试在我的代码中实现。我在我的 SmartApp< 中尝试在 "//STARTING HERE""//ENDING HERE" 评论之间运行代码时编写了下面的代码 class 它不启动我的 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))));
}
}
}

最佳答案

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

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

关于java - 导致错误的android偏好设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4870957/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com