Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8333411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:01:18+00:00 2026-06-09T03:01:18+00:00

Description: I am getting ClassCast Exception while i am clicking to button on the

  • 0

Description:

I am getting ClassCast Exception while i am clicking to button on the title bar.
My source code is as follows:

My activity code:

public void onClick(View v) {

    if (v.getId() == R.id.about_us) {

        if (isInternetConnected()) {
            System.out.println("about us");
            startActivity(new Intent(GridDisplayActivity.this, AboutUsActivity.class));
        } else {
            setAlertBox("No Internet Connection");
        }
    }
    if (v.getId() == R.id.hard_copy) {

        if (isInternetConnected()) {
            System.out.println("hard copy");
            startActivity(new Intent(GridDisplayActivity.this,HardCopyActivity.class));
        } else {
            setAlertBox("No Internet Connection");
        }
    }

}

Custom Application Code:

public class CustomApplication extends Application {

private Map<String, List<CustomAsyncTask<?,?,?>>> mActivityTaskMap;

public CustomApplication() {
    mActivityTaskMap = new HashMap<String, List<CustomAsyncTask<?,?,?>>>();
}

public void removeTask(CustomAsyncTask<?,?,?> task) {
    for (Entry<String, List<CustomAsyncTask<?,?,?>>> entry : mActivityTaskMap.entrySet()) {
        List<CustomAsyncTask<?,?,?>> tasks = entry.getValue();
        for (int i = 0; i < tasks.size(); i++) {
            if (tasks.get(i) == task) {
                tasks.remove(i);
                break;
            }
        }

        if (tasks.size() == 0) {
            mActivityTaskMap.remove(entry.getKey());
            return;
        }
    }
}

public void addTask(Activity activity, CustomAsyncTask<?,?,?> task) {
    String key = activity.getClass().getCanonicalName();
    List<CustomAsyncTask<?,?,?>> tasks = mActivityTaskMap.get(key);
    if (tasks == null) {
        tasks = new ArrayList<CustomAsyncTask<?,?,?>>();
        mActivityTaskMap.put(key, tasks);
    }

    tasks.add(task);
}

public void detach(Activity activity) {
    List<CustomAsyncTask<?,?,?>> tasks = mActivityTaskMap.get(activity.getClass().getCanonicalName());
    if (tasks != null) {
        for (CustomAsyncTask<?,?,?> task : tasks) {
            task.setActivity(null);
        }
    }
}

public void attach(Activity activity) {
    List<CustomAsyncTask<?,?,?>> tasks = mActivityTaskMap.get(activity.getClass().getCanonicalName());
    if (tasks != null) {
        for (CustomAsyncTask<?,?,?> task : tasks) {
            task.setActivity(activity);
        }
    }
  }

}

The logs that i am getting are:

07-26 11:06:10.567: E/AndroidRuntime(381):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-26 11:06:10.567: E/AndroidRuntime(381):  at dalvik.system.NativeStart.main(Native Method)
07-26 11:06:10.567: E/AndroidRuntime(381): Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.bespoken.android.provider.CustomApplication
07-26 11:06:10.567: E/AndroidRuntime(381):  at com.bespoken.android.GridDisplayActivity.onSaveInstanceState(GridDisplayActivity.java:567)
07-26 11:06:10.567: E/AndroidRuntime(381):  at android.app.Activity.performSaveInstanceState(Activity.java:1114)
07-26 11:06:10.567: E/AndroidRuntime(381):  at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1184)
 07-26 11:06:10.567: E/AndroidRuntime(381):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2469)
 07-26 11:06:10.567: E/AndroidRuntime(381):     ... 12 more

Please guide me.
Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:01:19+00:00Added an answer on June 9, 2026 at 3:01 am

    Don’t do (CustomApplication)getApplication() but instead have a static CustomApplication field, say mInstance, in the CustomApplication class, set it in the CustomApplication.onCreate to this and use it wherever you want to access CustomApplication's methods.

    class CustomApplication extends Application
    {
    
    private static CustomApplication mInstance;
    
    @Override
    void onCreate() {
        super.onCreate();
        mInstance = this;
    }
    
    public static CustomApplication getInstance() { return mInstance; }
    
    public void mySpecificMethod() { .... }
    
    }
    

    Then you call from where you need it:

    CustomApplication.getInstance()->mySpecificMethod()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that creates the meta-description. I'm getting a 0 as
Description : I am creating customized title bar. The codes for customization is as
I am getting the following error on my page: Security Exception Description: The application
i am getting exception over these lines of code dont know why its giving
See this simple code,But i am getting Description The method onClick(View) of type CheckboxActivity
Am getting Error inflating fragment error while running calling an Activity which having fragments.
With $.ajax , I'm getting a JSON response like follows: [{project:{name:P1111, description:I like chicken
How can i get Image tag from html String. I am getting description like
Description: An unhandled exception occurred during the execution of the current web request. Please
I'm getting the following exception when uploading a file to Rackspace Cloud Files: Security

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.