试图加载Spinner位置/值导致Integer.parseInt()崩溃

时间:2014-01-25 05:23:01

标签: android spinner

这里的android编程初学者。我正在编写一个具有性别/身高/体重设置页面的应用程序,并计算此信息并将其传递给另一个活动(主要活动)。

当用户返回设置页面时,它将加载用户之前输入的所有值。目前加载我的EditText字段没有问题,但性别是使用Spinner(男/女),当我输入代码时,Settings活动就崩溃了。

它似乎在Integer.parseInt()崩溃。

提前致谢!

按钮监听器的片段

btnSave = (Button) this.findViewById(R.id.btn_save);
btnSave.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
.
.
.
savePreferences("storedAge", inputAge.getText().toString());
savePreferences("storedHeight", inputHeight.getText().toString());
savePreferences("storedWeight", inputWeight.getText().toString());
savePreferences("storedBMR", String.valueOf(myBMR));


int userChoice = chooseGender.getSelectedItemPosition();
savePreferences("storedGender",Integer.toString(userChoice));

savePreferences

private void savePreferences(String key, String value) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}

loadPreferences

private void loadSavedPreferences(){
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);


String age = sharedPreferences.getString("storedAge", "");
inputAge.setText(age);

String weight = sharedPreferences.getString("storedWeight", "");
inputWeight.setText(weight);
String height = sharedPreferences.getString("storedHeight", "");
inputHeight.setText(height);
String gender = sharedPreferences.getString("storedGender", "");
Integer genderValue = Integer.parseInt(gender);
chooseGender.setSelection(genderValue);
}

尝试了阿米特的建议,但仍然遇到了崩溃。记录下面的猫:

01-31 13:43:29.576: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property
01-31 13:43:30.158: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property
01-31 13:43:54.516: E/ActivityThread(609): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cf08b0 that was originally bound here
01-31 13:43:54.516: E/ActivityThread(609): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cf08b0 that was originally bound here
01-31 13:43:54.516: E/ActivityThread(609):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
01-31 13:43:54.516: E/ActivityThread(609):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
01-31 13:43:54.516: E/ActivityThread(609):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
01-31 13:43:54.516: E/ActivityThread(609):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
01-31 13:43:54.516: E/ActivityThread(609):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
01-31 13:43:54.516: E/ActivityThread(609):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
01-31 13:43:54.516: E/ActivityThread(609):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
01-31 13:43:54.516: E/ActivityThread(609):  at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
01-31 13:43:54.516: E/ActivityThread(609):  at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
01-31 13:43:54.516: E/ActivityThread(609):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
01-31 13:43:54.516: E/ActivityThread(609):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
01-31 13:43:54.516: E/ActivityThread(609):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
01-31 13:43:54.516: E/ActivityThread(609):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-31 13:43:54.516: E/ActivityThread(609):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
01-31 13:43:54.516: E/ActivityThread(609):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-31 13:43:54.516: E/ActivityThread(609):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-31 13:43:54.516: E/ActivityThread(609):  at java.lang.Thread.run(Thread.java:856)
01-31 13:43:54.526: E/StrictMode(609): null
01-31 13:43:54.526: E/StrictMode(609): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cf08b0 that was originally bound here
01-31 13:43:54.526: E/StrictMode(609):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
01-31 13:43:54.526: E/StrictMode(609):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
01-31 13:43:54.526: E/StrictMode(609):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
01-31 13:43:54.526: E/StrictMode(609):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
01-31 13:43:54.526: E/StrictMode(609):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
01-31 13:43:54.526: E/StrictMode(609):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
01-31 13:43:54.526: E/StrictMode(609):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
01-31 13:43:54.526: E/StrictMode(609):  at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
01-31 13:43:54.526: E/StrictMode(609):  at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
01-31 13:43:54.526: E/StrictMode(609):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
01-31 13:43:54.526: E/StrictMode(609):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
01-31 13:43:54.526: E/StrictMode(609):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
01-31 13:43:54.526: E/StrictMode(609):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-31 13:43:54.526: E/StrictMode(609):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
01-31 13:43:54.526: E/StrictMode(609):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-31 13:43:54.526: E/StrictMode(609):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-31 13:43:54.526: E/StrictMode(609):  at java.lang.Thread.run(Thread.java:856)
01-31 13:43:54.546: E/ActivityThread(609): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40ce27f0 that was originally bound here
01-31 13:43:54.546: E/ActivityThread(609): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40ce27f0 that was originally bound here
01-31 13:43:54.546: E/ActivityThread(609):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
01-31 13:43:54.546: E/ActivityThread(609):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
01-31 13:43:54.546: E/ActivityThread(609):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
01-31 13:43:54.546: E/ActivityThread(609):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
01-31 13:43:54.546: E/ActivityThread(609):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
01-31 13:43:54.546: E/ActivityThread(609):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
01-31 13:43:54.546: E/ActivityThread(609):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
01-31 13:43:54.546: E/ActivityThread(609):  at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
01-31 13:43:54.546: E/ActivityThread(609):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
01-31 13:43:54.546: E/ActivityThread(609):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
01-31 13:43:54.546: E/ActivityThread(609):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
01-31 13:43:54.546: E/ActivityThread(609):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-31 13:43:54.546: E/ActivityThread(609):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
01-31 13:43:54.546: E/ActivityThread(609):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-31 13:43:54.546: E/ActivityThread(609):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-31 13:43:54.546: E/ActivityThread(609):  at java.lang.Thread.run(Thread.java:856)
01-31 13:43:54.567: E/StrictMode(609): null
01-31 13:43:54.567: E/StrictMode(609): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40ce27f0 that was originally bound here
01-31 13:43:54.567: E/StrictMode(609):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
01-31 13:43:54.567: E/StrictMode(609):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
01-31 13:43:54.567: E/StrictMode(609):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
01-31 13:43:54.567: E/StrictMode(609):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
01-31 13:43:54.567: E/StrictMode(609):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
01-31 13:43:54.567: E/StrictMode(609):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
01-31 13:43:54.567: E/StrictMode(609):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
01-31 13:43:54.567: E/StrictMode(609):  at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
01-31 13:43:54.567: E/StrictMode(609):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
01-31 13:43:54.567: E/StrictMode(609):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
01-31 13:43:54.567: E/StrictMode(609):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
01-31 13:43:54.567: E/StrictMode(609):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-31 13:43:54.567: E/StrictMode(609):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
01-31 13:43:54.567: E/StrictMode(609):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-31 13:43:54.567: E/StrictMode(609):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-31 13:43:54.567: E/StrictMode(609):  at java.lang.Thread.run(Thread.java:856)

1 个答案:

答案 0 :(得分:0)

在将“gender”字符串解析为Integer之前,请检查它是否为空或空白,然后使用它重新显示 setSelection()行 -

if (gender != null && gender.length() > 0) 
        {
            Integer genderValue = Integer.parseInt(gender);
            chooseGender.setSelection(genderValue, true);
        }