使用 Preferences.getString 时出现 Android ClassCastException

发布于 2024-10-15 03:51:50 字数 2522 浏览 2 评论 0原文

我正在制作动态壁纸并尝试添加一些用户首选项。如果我有这样的情况:

public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
    drawType = prefs.getString("drawtype", "0");
}

在完全不相关的行上运行应用程序时,我会收到 ClassCastException:

Line front = (Line) polyList.get(polyList.size()-1);

如果我注释掉以“drawType =”开头的行,则不会出现异常,并且应用程序工作正常(尽管用户设置没有执行任何操作)。

谁能解释一下,这似乎没有任何意义。如果您需要我的代码的更多部分,我很乐意提供。谢谢。

编辑:不确定如何获取堆栈跟踪,因此冒着无用的风险,这是我的 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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抹茶夏天i‖ 2024-10-22 03:51:50

我和室友重新审视了代码,并像“玩电脑”一样。我们通过以下事实解决了这个问题:在尝试用“三角形”填充它之前,我的 ArrayList 没有清除旧的“线”,因此出现了转换错误。我既感到愚蠢,又感到如释重负,一如既往。感谢大家的帮助。

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.

烈酒灼喉 2024-10-22 03:51:50

尽管您声明这是完全不相关的行,但我建议将drawType 专门设置为您的首选项中的同一字符串。

如果它仍然失败,我不得不认为 polyList 至少被 drawType 的副作用所践踏。

如果成功,则记录您从 prefs.getString(...) 返回的内容并确保它是您所期望的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文