Droid Fu Force Close - Java can't cast context to DroidFuApplication object

425 Views Asked by At

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?

1

There are 1 best solutions below

2
On BEST ANSWER

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>