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 8584735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:50:29+00:00 2026-06-11T21:50:29+00:00

I am working on an android app and am currently having a problem with

  • 0

I am working on an android app and am currently having a problem with passing contexts to a standard Java class from a ListFragment.

I have a class called Common and in the constructor I pass the context so I can do various common tasks, such as displaying an alert dialogue, so that I can keep reusing the code instead of having to write the code out each time for every alert dialog box I need. To initialise the class in a standard activity I am using.

Common common = new Common(this);

The code above works fine if this is done in a class that extends an Activity. However, if I want to do the same sort of thing but in a class that extends a ListFragment, this doesn’t work, so I use the following code in order initialise the class

Common common = new Common(getActivity().getApplicationContext());

When the above code is executed in the ListFragment, when a function is used to display a Yes/No alert dialogue I get a force close with the exception

FATAL EXCEPTION: main
android.view.WindowManager$BadTokenException: Unable to add window
--token null is not for an application

Below is the code for the constructor for the class

public Common(Context context)
{
    this.context = context;
}

Just to reiterate, all of the functions within the Common class, including the Yes/No dialogue work fine without problems if the Common class is initialised from a class that extends Activity using the this argument passed to the constructor. Its only if I getActivity().getApplicationContext() as an argument passed to the constructor that I get this error.

As a test I have also changed one my classes that extends an activity and used the getApplicationContext instead of using this, and I get the same error, so its not necessarily specific to me using a ListFragment.

Thanks for any help you can provide.

  • 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-11T21:50:31+00:00Added an answer on June 11, 2026 at 9:50 pm

    You can not use ApplicationContext in your case. Instead use just getActivity(). Activity is a Context so your Common class constructor will be satisfied.

    But your Common class should really have Common(Activity a) constructor instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently working on an Android app and I am having some issues
I am fairly new to java having come from a VS2010 background. Currently working
I'm currently working on an Android project, where I have to collect data from
I am currently working on an Android App, and have been for about six
I am currently working on an Android app and I am trying to get
I am working on an android app, and I am currently trying to create
Working on an Android app with FB 4.6, I would like to have the
I am working on an android app, and I am having difficulties with ListActivity
I'm working on an Android app native written in java and I'm getting a
I am working on an android app and have to use remote database for

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.