【发布时间】:2014-04-15 13:44:18
【问题描述】:
我正在尝试在我的应用中添加一个 SettingActivity 来设置一些数字数据(http 请求超时)。
我把这个 xml 文件创建到 res --> xml
prefs.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:summary="Timeouts for the HTTP requests"
android:title="Timeouts">
<EditTextPreference
android:key="track_connection_timeout"
android:title="Track connection timeout"
android:inputType="number"
android:digits="0123456789"
android:defaultValue="10000"/>
<EditTextPreference
android:key="track_download_timeout"
android:title="Track download timeout"
android:inputType="number"
android:digits="0123456789"
android:defaultValue="20000"/>
<EditTextPreference
android:key="elevation_connection_timeout"
android:title="Elevation connection timeout"
android:inputType="number"
android:digits="0123456789"
android:defaultValue="10000"/>
<EditTextPreference
android:key="elevation_download_timeout"
android:title="Elevation download timeout"
android:inputType="number"
android:digits="0123456789"
android:defaultValue="10000"/>
</PreferenceCategory>
如您所见,我正在尝试将 only 数字放入 EditTexts(没有成功)。
我也创建了这两个类:
设置活动
public class SettingsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
}
设置片段
public class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.prefs);
}
}
在 MainActivity 我有这个:
private int trackConnectionTimeout;
private int trackDataTimeout;
private int elevationConnectionTimeout;
private int elevationDataTimeout;
@Override
protected void onCreate(Bundle savedInstanceState) {
....
//Caricamento delle preferenze
PreferenceManager.setDefaultValues(MainActivity.this, R.xml.prefs, false);
loadPreferences();
...
}
private void loadPreferences() {
SharedPreferences sp = PreferenceManager.
getDefaultSharedPreferences(MainActivity.this);
trackConnectionTimeout = Integer.parseInt(sp.getString("track_connection_timeout",null));
trackDataTimeout = Integer.parseInt(sp.getString("track_download_timeout",null));
elevationConnectionTimeout = Integer.parseInt(sp.getString("elevation_connection_timeout",null));
elevationDataTimeout = Integer.parseInt(sp.getString("elevation_download_timeout",null));
}
问题有两个:
1) 此解决方案适用于 API 11 或更高版本。我希望它可以与所有版本兼容。
2) 当应用程序启动时,我得到:“NumberFormatException”,因为它不可能将 null 值转换为 int 值,但我设置了默认值!
谢谢。
更新:
我重复一遍:
如果我进入设置并取消 EditText 的内容,在应用程序启动时我会得到:java.lang.NumberFormatException: Invalid int: ""。因此,其他值不会被初始化。
例如,我决定取消包含值 trackConnectionTimeout 的 EditTextPreference 的内容。
我关闭应用程序并重新打开它 --> 我明白了:
04-15 17:54:46.867 10999-10999/com.loris.stefano.easyroutes W/System.err: java.lang.NumberFormatException: Invalid int: "" 04-15 17:54:46.877 10999-10999 /com.loris.stefano.easyroutes W/System.err:在 java.lang.Integer.invalidInt(Integer.java:138) 04-15 17:54:46.877 10999-10999/com.loris.stefano.easyroutes W/ System.err:在 java.lang.Integer.parseInt(Integer.java:359) 04-15 17:54:46.877 10999-10999/com.loris.stefano.easyroutes W/System.err:在 java.lang.Integer .parseInt(Integer.java:332) 04-15 17:54:46.877 10999-10999/com.loris.stefano.easyroutes W/System.err:在 com.loris.stefano.easyroutes.main.MainActivity.loadPreferences(MainActivity .java:235) 04-15 17:54:46.877 10999-10999/com.loris.stefano.easyroutes W/System.err﹕在 com.loris.stefano.easyroutes.main.MainActivity.onResume(MainActivity.java:431 ) 04-15 17:54:46.877 10999-10999/com.loris.stefano.easyroutes W/System.err﹕在 android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1184) 04-15 17:54:46.877 10999 -10999/com .loris.stefano.easyroutes W/System.err:在 android.app.Activity.performResume(Activity.java:5082) 04-15 17:54:46.877 10999-10999/com.loris.stefano.easyroutes W/System。错误:在 android.app.ActivityThread.performResumeActivity(ActivityThread.java:2586) 04-15 17:54:46.877 10999-10999/com.loris.stefano.easyroutes W/System.err:在 android.app.ActivityThread.handleResumeActivity (ActivityThread.java:2624) 04-15 17:54:46.877 10999-10999/com.loris.stefano.easyroutes W/System.err:在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1249) 04 -15 17:54:46.877 10999-10999/com.loris.stefano.easyroutes W/System.err:在 android.os.Handler.dispatchMessage(Handler.java:99) 04-15 17:54:46.877 10999-10999 /com.loris.stefano.easyroutes W/System.err:在 android.os.Looper.loop(Looper.java:213) 04-15 17:54:46.877 10999-10999/com.loris.stefano.easyroutes W/ System.err:在 android.app.ActivityThread.main(ActivityThread.java:4787) 04-15 17:54:46.877 10999-10999/com.loris.stefano.easyroutes W/System.err : 在 java.lang.reflect.Method.invokeNative(Native Method) 04-15 17:54:46.877 10999-10999/com.loris.stefano.easyroutes W/System.err: 在 java.lang.reflect.Method.invoke (Method.java:511) 04-15 17:54:46.887 10999-10999/com.loris.stefano.easyroutes W/System.err﹕在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java :789) 04-15 17:54:46.887 10999-10999/com.loris.stefano.easyroutes W/System.err: 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 04-15 17:54:46.887 10999-10999/com.loris.stefano.easyroutes W/System.err:在 dalvik.system.NativeStart.main(本机方法)
我可以捕捉到这个异常,但其他值仍然是“null”。
【问题讨论】:
-
关于问题 #1:您的代码的哪一部分仅适用于 API 11+?
标签: android preferenceactivity