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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:14:35+00:00 2026-05-19T03:14:35+00:00

I made a class to handle a simple message popup so I can reuse

  • 0

I made a class to handle a simple message popup so I can reuse the code throughout the app. I can’t seem to be able to get the context right. This is called from all over the place and often from classes that does not have a UI directly. See the line below…

public class msg  {

    public void msghand(String message, Exception e) {
    {

        String s;

        if (e != null) 
        {
            s=  message + "\n" + e.getLocalizedMessage() + " " + e.toString();
        }
        else
        {
            s= message ;
        }

        new AlertDialog.Builder(  getApplicationContext () )  <<<< HERE IS THE PROBLEM
        .setMessage(s)

        .setPositiveButton("OK", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int whichButton) {
            }
        })
        .create()
        .show();


    }

    }
}
  • 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-05-19T03:14:36+00:00Added an answer on May 19, 2026 at 3:14 am

    Is it possible for you to pass the Context in as a parameter?

    public void msghand(String message, Exception e, Context context) {
        ...
        new AlertDialog.Builder(context)
        ...
    

    Where are you performing work without a Context? Services do not have a UI, but still have a Context.

    Edit:

    You could create a small message service that is statically accessible, and created when your application starts. For example:

    class MyActivity extends Activity
    {
        public void onCreate(Bundle savedInstanceState)
        {
            // create the Message service that can be statically accessed
            s_MessageService = new MessageService(getApplicationContext());
            ...
        }
    
        public static MessageService getApplicationMessageService()
        {
            return s_MessageService;
        }
    
        private static MessageService s_MessageService;
    }
    

    Where MessageService is implemented appropriately

    class MessageService
    {
        public MessageService(Context messageContext)
        {
            m_MyContext = messageContext;
        }
    
        public msghand(String message, Exception e)
        {
            // exactly the same as before, except using the stored context
        }
    
        Context m_MyContext = null;
    }
    

    Your DBHelper class could use it via

    MyActivity.getApplicationMessageService().msghand(...);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I unit test my NSURLConnection delegate? I made a ConnectionDelegate class which
EDIT: I have made a clean, new project, but still can't get it working.
I might be asking very simple question, but i am not able to get
I have a simple class that handles the connection being made between a client
I made a class library, and put it in GAC. It has one C#
I made a class which looks something like. class NewInt: def __init__(self, x,y): self.holders
I have made an class which conforms to the NSCoding protocol and does all
So, I made a class that takes arrays and calculates a value from them.
I am learning C++ and I have a question. I made a class in
I made a form class in c# that has a devexpress grid, a label

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.