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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:06:18+00:00 2026-05-28T00:06:18+00:00

I would like to access hashmap set of values created in main class from

  • 0

I would like to access hashmap set of values created in main class from other class.I have followed the steps for it but I am only getting null value at the sub-class.Here is the code

public class SoapTester extends Activity {  
private static final String TAG = "Test";  
public HashMap<String, String> map = new HashMap<String, String>();

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    map.put("year", "Apple");
    map.put("make", "Mango");
    map.put("model", "Grape");
    map.put("style", "Orange");
    map.put("series", "Peach");
}

public HashMap<String, String> getHashmap() {
    Log.v(TAG, "map2: E" + map);
    return map;
}

public void setHashmap(HashMap<String, String> map) {
    this.map = map;
    getHashmap();
    Log.v(TAG, "map1: E" + map);
}
}

//Sub Class

public class Tradein extends Activity {
private static final String TAG = "Test";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tradein);
    SoapTester ex = new SoapTester();
    HashMap<String, String> hm = ex.getHashmap();
    Log.v(TAG, "hm: " + hm);//Getting Null Value here
}
}

Have I missed anything?

  • 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-28T00:06:19+00:00Added an answer on May 28, 2026 at 12:06 am

    make the HashMap static

    public static HashMap<String, String> map = new HashMap<String, String>(); 
    

    In this way we can change values in any activity at will, regardless of the exact flow of control between the various activities.

    Note that this trick can only be used if you don’t care about the instantiation of more than one copy of the same activity (class) in the application, this is the easiest to implement

    Step 2 : Android; Implementing global state; share data between Activities and across your application

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

Sidebar

Related Questions

I would like to access an ActionScript 3.0 top level function from a class
I would like to access a class everywhere in my application, how can I
I have a list of objects and I would like to access the objects
I would like access to the page name from within a page. For example
I would like to access my AudioUnit Component Kernel members from an action method
Does C# have it's own kind of database system like Access/ADO alternative. I would
I would like to access profiles table from my Profile model which belongs_to :user
i would like to access an variable, which is in an class (not as
I would like to have a HashMap<String, String> , that every day at midnight,
I would like to access some functions defined in another PHP file, but that

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.