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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:18:30+00:00 2026-06-06T06:18:30+00:00

In my application, i need to declare an Array based on the value stored

  • 0

In my application, i need to declare an Array based on the value stored by the user in the SharedPreference variable. The problem is that the array needs to be declared in a static block as the size of the array needs to be declared before the onCreate() is called in my class.

I have an ExpandableList in my Activity with parent array as the dates of the next seven days.

static int plannerSlotCount=7;
static public String[] parent = new String[plannerSlotCount];
static
{

    Calendar cal = Calendar.getInstance();
    String strdate = null;
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");

    int i;
    for(i=0;i<plannerSlotCount;i++)
    {

        if (cal != null) {
            strdate = sdf.format(cal.getTime());
            }
            parent[i] = strdate;
            cal.add(Calendar.HOUR_OF_DAY,24);
    }

}

If i don’t declare the array inside the static block, then I get an error at

public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        TextView textView = getGenericView();
        textView.setText(getGroup(groupPosition).toString());
        return textView;
    }

So,I have to declare the content of the array in the static block itself I guess.

The thing is that I want to change the number of days to display(it is currently set to 7). So, I thought of saving the number in a SharedPreference variable and accessing it to initialize the array.

The problem I am facing, is that

    SharedPreferences preferences = getSharedPreferences(Settings.PREF_SETTINGS_FILE_NAME, MODE_PRIVATE);
    final int slotCounter = preferences.getInt("slotCount", 7);

gives me an Error saying that

Cannot make a static reference to the non-static method getSharedPreferences(String, int) from the type ContextWrapper

Is there any possible way to achieve this ?

  • 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-06T06:18:32+00:00Added an answer on June 6, 2026 at 6:18 am

    no, you can not. Since the

    static {
    }
    

    blocks are called when you refers the first time the class. So, I think, before the onCreate si called. In order to accesss SharedPreference you need a context, and the context is valid after the onCreate . So you can not access SharedPreference in the static block

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

Sidebar

Related Questions

I have two application that need to talk to each other. App1 needs to
I need a dynamically allocated bi-dimensional array of objects. I declared a static pointer
I'm trying to understand how the Application class. I've noticed that need to declare
I'm working on a embedded application where I need to declare an array of
I need to impersonate a user in a VB.NET 2008 WinForms application, so that
I have this application that need to do some things in protected paths (like
I'm working on some routes for my CodeIgniter application, and I need to declare
In my application I need to call a Stored Proc Asynchronously. For this I
I have a need to set a collection of name/value pairs inside my application
I have an application that contains 4 mutable arrays,i already displays one array of

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.