1

Android的喜好我想一些喜好的能力加入到我的應用程序。我希望應用程序能夠加載在XML文件中設置的默認值,然後能夠稍後更改這些值。我碰到了首選項,並試圖在我的代碼中實現。我制定了下面的代碼,當試圖運行「//開始於此」「// ENDING HERE」之間的代碼時評論在我的SmartApp類它不啓動我的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)))); 
     } 
    } 
} 

回答

2

我認爲這個代碼有幾個問題。首先是從您的首選項加載時,您擁有的默認值全部爲null。對於Integer#parseInt()的文檔指出它會拋出一個NumberFormatException如果你null通過。改爲使用0或其他值。

第二個是,我認爲到dR方法,所有的呼叫都需要很長的時間才能完成。這些功能是做什麼的?我敢打賭,他們是有點密集的任務,而且由於他們在你onCreate()方法的操作系統認爲您的應用程序已花了太長的啓動,從而殺死它。將這些呼叫移出到AsyncTask,以便它們可以在後臺加載。

+0

災難恢復方法不考慮長期的。它只是將這些值分配給私有值類。你知道我喜歡接受在xml中設置的默認值嗎?我認爲將它設置爲null會使它從xml文件中取得默認值,而不是在parseInt中設置的值。會讓你知道我的結果。 – prolink007 2011-02-02 16:22:41