我有这段代码从文件中检索数据然后进行一些处理,最后在EditTextPreference中设置结果。
try {
//open the specified input file and create a reader
FileInputStream fIn = context.openFileInput(fileDir+fileName);
InputStreamReader ipsr = new InputStreamReader(fIn);
BufferedReader b = new BufferedReader(ipsr);
String ligne;
while ((ligne = b.readLine()) != null) {
String prix = ligne.split(" ")[2];
//prix = prix.substring(0, prix.length() - 2);
Log.d("PRIX",prix);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String prix_total = settings.getString("prix_total", "0");
int a= Integer.valueOf(prix_total);
int c= Integer.valueOf(prix);
a= c + a;
String z=String.valueOf(a);
Log.d("A",z);
EditTextPreference myEditTextPreference = (EditTextPreference) findPreference("prix_total");
myEditTextPreference.setText(z);
}
//done, cleanup and return
fIn.close();
ipsr.close();
}
catch (Exception e)
{
Log.e("blah", "Exception", e);
}
但是,我遇到了这个问题(logcat):
09-12 01:09:10.941: ERROR/blah(10866): Exception
09-12 01:09:10.941: ERROR/blah(10866): java.lang.NumberFormatException: unable to parse '' as integer
09-12 01:09:10.941: ERROR/blah(10866): at java.lang.Integer.parseInt(Integer.java:362)
09-12 01:09:10.941: ERROR/blah(10866): at java.lang.Integer.parseInt(Integer.java:332)
09-12 01:09:10.941: ERROR/blah(10866): at java.lang.Integer.valueOf(Integer.java:506)
09-12 01:09:10.941: ERROR/blah(10866): at carburant.android.com.Settings.onCreate(Settings.java:46)
09-12 01:09:10.941: ERROR/blah(10866): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-12 01:09:10.941: ERROR/blah(10866): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
09-12 01:09:10.941: ERROR/blah(10866): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
09-12 01:09:10.941: ERROR/blah(10866): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-12 01:09:10.941: ERROR/blah(10866): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
09-12 01:09:10.941: ERROR/blah(10866): at android.os.Handler.dispatchMessage(Handler.java:99)
09-12 01:09:10.941: ERROR/blah(10866): at android.os.Looper.loop(Looper.java:130)
09-12 01:09:10.941: ERROR/blah(10866): at android.app.ActivityThread.main(ActivityThread.java:3687)
09-12 01:09:10.941: ERROR/blah(10866): at java.lang.reflect.Method.invokeNative(Native Method)
09-12 01:09:10.941: ERROR/blah(10866): at java.lang.reflect.Method.invoke(Method.java:507)
09-12 01:09:10.941: ERROR/blah(10866): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
09-12 01:09:10.941: ERROR/blah(10866): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
09-12 01:09:10.941: ERROR/blah(10866): at dalvik.system.NativeStart.main(Native Method)
可能是什么问题?我调试了“prix”,并确定它输出''
:.
谢谢你的帮助。
PS:数据具有以下形式:
data = date + ": " + y + "L/100KM "+ " " + value1 + "dt "+ value2 + "KM\n";
答案 0 :(得分:1)
valueOf和parseInt无法处理空字符串或空值。添加一些检查,例如使用TextUtils.IsEmpty(string)来验证您没有传递空数据。
另外我看到你用try块来解决这个问题,考虑添加一个catch(NumberFormatException ex)块并记录错误,如果它永远不应该为空。