문제

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))));
        }
    }
}
도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top