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

  • Home
  • SEARCH
  • 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

I made a class from Linq to SQL Clasees with VS 2008 SP1 Framework
I made a class that derives from Component: public class MyComponent: System.ComponentModel.Component { }
I've made a class which inherits from UserControl, but which I only want to
I have a class made up of several fields, and I have several constructors.
I have to use a class/assembly made in C# .NET from native C++ application.
I've made some unit tests (in test class). The tutorial I've read said that
I have a JavaScript class that I have made and put it into its
Recently I've been designing a Thread class library, I've made a Thread abstract class
I'm creating my first class, mainly guided by Overland's C++ Without Fear. I've made
In my webapplication (C#, .Net 3.5), made up of a core class library (containing

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.