Droid Fu 强制关闭 - Java 无法将上下文转换为 DroidFuApplication 对象

发布于 2024-12-21 11:48:30 字数 1441 浏览 1 评论 0原文

我是新来的,刚刚学会了如何编写 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 技术交流群。

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

发布评论

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

评论(1

彩扇题诗 2024-12-28 11:48:30

我从未使用过 DroidFu,但请确保您已将下一行添加到 AndrodiManifest.xml

<manifest ...>
   ...
   <application .... android:name="com.github.droidfu.DroidFuApplication">
      ...
   </application>
   ...
</manifest>

I never used DroidFu, but make sure you've added next line to you AndrodiManifest.xml:

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