无法设置EditTextPreference [ForceClose]

时间:2011-08-17 21:13:33

标签: android sharedpreferences

我想将一些数据存储到文件中,然后从EditTextPreference中取一个值并将其设置为另一个,看起来很简单但不幸的是我总是有一个FC: EditTextPreference prixtotal =(EditTextPreference)findPreference(“total_prix”);

EditTextPreference prixtotal = (EditTextPreference) findPreference("total_prix");                     
if ((CheckboxAir==true) && (CheckboxHuile==true) && (CheckboxCarburant==true))
{
 data_vidange = date + ": " + km + "km" + " " + prix + "dt"+ " " + " F.Air" + " F.Huile" + " F.Carburant" + "\n";
MyIO.WriteSettings(context,fileDir+fileName, data_vidange);
prixtotal.setText(prix);
}

logcat的:

08-17 21:08:39.677: ERROR/AndroidRuntime(386): FATAL EXCEPTION: main
08-17 21:08:39.677: ERROR/AndroidRuntime(386): java.lang.NullPointerException
08-17 21:08:39.677: ERROR/AndroidRuntime(386):     at carburant.android.com.Vidange.onOptionsItemSelected(Vidange.java:109)
08-17 21:08:39.677: ERROR/AndroidRuntime(386):     at android.app.Activity.onMenuItemSelected(Activity.java:2205)
08-17 21:08:39.677: ERROR/AndroidRuntime(386):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:748)
08-17 21:08:39.677: ERROR/AndroidRuntime(386):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
08-17 21:08:39.677: ERROR/AndroidRuntime(386):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
08-17 21:08:39.677: ERROR/AndroidRuntime(386):     at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)
08-17 21:08:39.677: ERROR/AndroidRuntime(386):     at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
08-17 21:08:39.677: ERROR/AndroidRuntime(386):     at android.view.View$PerformClick.run(View.java:9080)
08-17 21:08:39.677: ERROR/AndroidRuntime(386):     at android.os.Handler.handleCallback(Handler.java:587)
08-17 21:08:39.677: ERROR/AndroidRuntime(386):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-17 21:08:39.677: ERROR/AndroidRuntime(386):     at android.os.Looper.loop(Looper.java:123)
08-17 21:08:39.677: ERROR/AndroidRuntime(386):     at android.app.ActivityThread.main(ActivityThread.java:3683)
08-17 21:08:39.677: ERROR/AndroidRuntime(386):     at java.lang.reflect.Method.invokeNative(Native Method)
08-17 21:08:39.677: ERROR/AndroidRuntime(386):     at java.lang.reflect.Method.invoke(Method.java:507)
08-17 21:08:39.677: ERROR/AndroidRuntime(386):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-17 21:08:39.677: ERROR/AndroidRuntime(386):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-17 21:08:39.677: ERROR/AndroidRuntime(386):     at dalvik.system.NativeStart.main(Native Method)

第109行是:prixtotal.setText(prix);

任何人都知道问题的根源? 谢谢。

1 个答案:

答案 0 :(得分:0)

你在android中找到一个视图(即一个EditText框)的方式是说 TextView tv = (TextView) findViewById(R.id.viewname);

我不确定你的findPreference()方法是做什么的,但我的猜测是那里有错误。