我有一个很奇怪的问题。首先请看一下我的代码。我已经创建了我的自定义类,它扩展了EditTextPreference,并且我覆盖了onClick(DialogInterface,which)方法。
@Override
public void onClick(DialogInterface dialog, int which) {
super.onClick(dialog, which);
switch(which) {
case DialogInterface.BUTTON_POSITIVE:
long currentDelay = (Long.parseLong(getEditText().getText().toString())*1000);
if(currentDelay < 0.5) {
getEditText().setText("1000");
currentDelay = 1000;
}
persistLong(currentDelay); // Crashes (Even if I put literal inside)
persistString(getEditText().getText().toString()); // It works
break;
}
如果执行onclick方法,persistLong总是崩溃我的程序并抛出ClassCastException。即使我把文字放在参数中(比如,persistLong(4000))。另一方面,persistString工作正常。
这可能是什么问题?有什么建议吗?
编辑:我的LogCat:第59行是persistLong方法的地方。
03-05 15:51:14.212: E/AndroidRuntime(22739): FATAL EXCEPTION: main
03-05 15:51:14.212: E/AndroidRuntime(22739): java.lang.ClassCastException: java.lang.String
03-05 15:51:14.212: E/AndroidRuntime(22739): at android.app.ContextImpl$SharedPreferencesImpl.getLong(ContextImpl.java:2939)
03-05 15:51:14.212: E/AndroidRuntime(22739): at android.preference.Preference.getPersistedLong(Preference.java:1388)
03-05 15:51:14.212: E/AndroidRuntime(22739): at android.preference.Preference.persistLong(Preference.java:1359)
03-05 15:51:14.212: E/AndroidRuntime(22739): at com.callandsmsled.MyEditTextPreference.onClick(MyEditTextPreference.java:59)
03-05 15:51:14.212: E/AndroidRuntime(22739): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159)
03-05 15:51:14.212: E/AndroidRuntime(22739): at android.os.Handler.dispatchMessage(Handler.java:99)
03-05 15:51:14.212: E/AndroidRuntime(22739): at android.os.Looper.loop(Looper.java:130)
03-05 15:51:14.212: E/AndroidRuntime(22739): at android.app.ActivityThread.main(ActivityThread.java:3835)
03-05 15:51:14.212: E/AndroidRuntime(22739): at java.lang.reflect.Method.invokeNative(Native Method)
03-05 15:51:14.212: E/AndroidRuntime(22739): at java.lang.reflect.Method.invoke(Method.java:507)
03-05 15:51:14.212: E/AndroidRuntime(22739): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
03-05 15:51:14.212: E/AndroidRuntime(22739): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
03-05 15:51:14.212: E/AndroidRuntime(22739): at dalvik.system.NativeStart.main(Native Method)