I'm making a Live Wallpaper and attempting to add some user preferences. If I have this:
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
drawType = prefs.getString("drawtype", "0");
}
I get a ClassCastException upon running the app on a completely unrelated line:
Line front = (Line) polyList.get(polyList.size()-1);
If I comment out the line beginning with "drawType =" I get no exceptions and the app works fine (albeit without the user settings doing anything).
Can anybody explain this at all, it doesn't seem to make any sense. If you need more parts of my code I'm happy to provide them. Thanks.
EDIT: Not sure how to get a stack trace, so at the risk of being unhelpful, here is my logcat:
02-01 21:45:26.934: ERROR/AndroidRuntime(4264): FATAL EXCEPTION: main
02-01 21:45:26.934: ERROR/AndroidRuntime(4264): java.lang.ClassCastException: com.nickavv.linear.Line
02-01 21:45:26.934: ERROR/AndroidRuntime(4264): at com.nickavv.linear.Wallpaper.draw(Wallpaper.java:127)
02-01 21:45:26.934: ERROR/AndroidRuntime(4264): at com.nickavv.linear.Wallpaper$WallEngine.drawFrame(Wallpaper.java:95)
02-01 21:45:26.934: ERROR/AndroidRuntime(4264): at com.nickavv.linear.Wallbaser$AnimationEngine.onSurfaceChanged(Wallbaser.java:45)
02-01 21:45:26.934: ERROR/AndroidRuntime(4264): at android.service.wallpaper.WallpaperService$Engine.updateSurface(WallpaperService.java:558)
02-01 21:45:26.934: ERROR/AndroidRuntime(4264): at android.service.wallpaper.WallpaperService$Engine.attach(WallpaperService.java:606)
02-01 21:45:26.934: ERROR/AndroidRuntime(4264): at android.service.wallpaper.WallpaperService$IWallpaperEngineWrapper.executeMessage(WallpaperService.java:817)
02-01 21:45:26.934: ERROR/AndroidRuntime(4264): at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:61)
02-01 21:45:26.934: ERROR/AndroidRuntime(4264): at android.os.Handler.dispatchMessage(Handler.java:99)
02-01 21:45:26.934: ERROR/AndroidRuntime(4264): at android.os.Looper.loop(Looper.java:123)
02-01 21:45:26.934: ERROR/AndroidRuntime(4264): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-01 21:45:26.934: ERROR/AndroidRuntime(4264): at java.lang.reflect.Method.invokeNative(Native Method)
02-01 21:45:26.934: ERROR/AndroidRuntime(4264): at java.lang.reflect.Method.invoke(Method.java:521)
02-01 21:45:26.934: ERROR/AndroidRuntime(4264): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-01 21:45:26.934: ERROR/AndroidRuntime(4264): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-01 21:45开发者_开发技巧:26.934: ERROR/AndroidRuntime(4264): at dalvik.system.NativeStart.main(Native Method)
My roommate and I took a fresh look over the code and "played computer" as it were. We solved it by the fact that my ArrayList wasn't being cleared of the old "Line"s before attempting to fill it with "Triangle"s, thus the casting error. I feel both stupid and relieved at the same time, as it always it. Thanks for all the help everyone.
Despite your stating that this is a completely unrelated line, I suggest setting drawType specifically to the same string that is in your prefs.
if it still fails, I'd have to think that polyList is getting trampled by at least a side-effect of drawType.
if it succeeds, then log what you're getting back from prefs.getString(...) and ensure that it is what you expect.
精彩评论