Droid Fu 强制关闭 - Java 无法将上下文转换为 DroidFuApplication 对象
我是新来的,刚刚学会了如何编写 Android 应用程序。
现在,我想用 Droid Fu 库制作一个应用程序 (这是一个很好的库,但缺乏文档和示例)。我的handleApplicationClosing 函数遇到问题。当我按下应用程序上的后退按钮时,就会出现问题,这意味着应用程序将关闭(因为我只尝试使用 droid fu 测试应用程序的 1 个活动)。
经过多次尝试调试程序,我发现当我的代码到达此函数时发生错误:
static void handleApplicationClosing(final Context context, int keyCode) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
ActivityManager am = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = am.getRunningTasks(2);
RunningTaskInfo currentTask = tasks.get(0);
RunningTaskInfo nextTask = tasks.get(1);
// if we're looking at this application's base/launcher Activity,
// and the next task is the Android home screen, then we know we're
// about to close the app
if (currentTask.topActivity.equals(currentTask.baseActivity)
&& nextTask.baseActivity.getPackageName().startsWith("com.android.launcher")) {
DroidFuApplication application = (DroidFuApplication) context
.getApplicationContext();
application.onClose();
}
}
}
在这一行:
DroidFuApplication application = (DroidFuApplication) context
.getApplicationContext();
错误代码是:
java.lang 类 ClassCastException
这是什么意思?
I'm new here, and I just learned how to code an Android application.
Right now, I want to make an Application with Droid Fu library
(which is a good library but has a lack of documentation and examples). I'm facing a problem with my handleApplicationClosing function. The problem occurs when I press the back button on my application, which mean the application closing (because I just try with only 1 activity for my testing application with droid fu).
After many tries to debug the program, I found the error occurred when my code reaches this function:
static void handleApplicationClosing(final Context context, int keyCode) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
ActivityManager am = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = am.getRunningTasks(2);
RunningTaskInfo currentTask = tasks.get(0);
RunningTaskInfo nextTask = tasks.get(1);
// if we're looking at this application's base/launcher Activity,
// and the next task is the Android home screen, then we know we're
// about to close the app
if (currentTask.topActivity.equals(currentTask.baseActivity)
&& nextTask.baseActivity.getPackageName().startsWith("com.android.launcher")) {
DroidFuApplication application = (DroidFuApplication) context
.getApplicationContext();
application.onClose();
}
}
}
on this line:
DroidFuApplication application = (DroidFuApplication) context
.getApplicationContext();
The error code was:
java.lang Class ClassCastException
What does this mean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从未使用过
DroidFu
,但请确保您已将下一行添加到AndrodiManifest.xml
:I never used
DroidFu
, but make sure you've added next line to youAndrodiManifest.xml
: