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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:34:05+00:00 2026-06-13T21:34:05+00:00

Can anyone please correct my codes. I’m getting error The method getApplicationContext() is undefined

  • 0

Can anyone please correct my codes. I’m getting error “The method getApplicationContext() is undefined for the type FreeFragment" and "The method getSystemService(String) is undefined for the type FreeFragment`” . I want it to check for internet connection before the Asynctask Runs.

public class FreeFragment extends SherlockListFragment implements
            ActionBar.TabListener {

        static final String URL = "https://myxml.xml";
        static final String KEY_SONG = "song";
        static final String KEY_ID = "id";
        static final String KEY_TITLE = "title";
        static final String KEY_CAT_ARTIST = "artistcat";
        static final String KEY_DURATION = "duration";
        static final String KEY_THUMB_URL = "thumb_url";
        static final String KEY_BIG_URL = "big_url";
        static final String KEY_CAT_URL = "cat_url";
        static final String KEY_DESC = "cat_desc";
        ArrayList<HashMap<String, String>> menuItems;
        ListAdapter adapter;

        private Fragment mFragment;

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            if (!isNetworkAvailable()) {
                Toast.makeText(getApplicationContext(),
                        "Internet Connection Required", Toast.LENGTH_SHORT).show();
                //finish(context);
            } else { 
            new loadListView().execute();
            }
            super.onActivityCreated(savedInstanceState);
        }


         private boolean isNetworkAvailable() {
             ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
             NetworkInfo activeNetworkInfo = connectivityManager
                     .getActiveNetworkInfo();
             return activeNetworkInfo != null;
         }

        public class loadListView extends AsyncTask<Integer, String, String> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected String doInBackground(Integer... args) {
                // updating UI from Background Thread
                menuItems = new ArrayList<HashMap<String, String>>();

                XMLParser parser = new XMLParser();
                String xml = parser.getXmlFromUrl(URL); // getting XML
                Document doc = parser.getDomElement(xml); // getting DOM element

                NodeList nl = doc.getElementsByTagName(KEY_SONG);
                // looping through all item nodes <item>
                for (int i = 0; i < nl.getLength(); i++) {
                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();
                    Element e = (Element) nl.item(i);
                    // adding each child node to HashMap key => value
                    map.put(KEY_ID, parser.getValue(e, KEY_ID));
                    map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
                    map.put(KEY_CAT_ARTIST, parser.getValue(e, KEY_CAT_ARTIST));
                    map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
                    map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
                    map.put(KEY_BIG_URL, parser.getValue(e, KEY_BIG_URL));
                    map.put(KEY_CAT_URL, parser.getValue(e, KEY_CAT_URL));
                    map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
                    // adding HashList to ArrayList
                    menuItems.add(map);
                }
                return null;
            }

            @Override
            protected void onPostExecute(String args) {

                if (getActivity() != null) {
                    adapter = new MainPageLazyAdapter(getActivity(), menuItems);
                    setListAdapter(adapter);

                    ListView lv = getListView();

                    lv.setOnItemClickListener(new OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
                            String thumburl;
                            String bigurl;
                            String caturl;
                            String title;
                            String desc;
                            String artist;
                            Intent in;

                            title = ((TextView) view.findViewById(R.id.title))
                                    .getText().toString();
                            artist = ((TextView) view.findViewById(R.id.artist))
                                    .getText().toString();
                            desc = ((TextView) view.findViewById(R.id.desc))
                                    .getText().toString();
                            thumburl = ((TextView) view
                                    .findViewById(R.id.thumb_image)).getText()
                                    .toString();
                            caturl = ((TextView) view.findViewById(R.id.cat))
                                    .getText().toString();
                            bigurl = ((TextView) view.findViewById(R.id.big_image))
                                    .getText().toString();

                            in = new Intent(getActivity(),
                                    SingleImageViewActivity.class);

                            in.putExtra(KEY_TITLE, title);
                            in.putExtra(KEY_DESC, desc);
                            in.putExtra(KEY_CAT_URL, caturl);
                            in.putExtra(KEY_THUMB_URL, thumburl);
                            in.putExtra(KEY_BIG_URL, bigurl);
                            in.putExtra(KEY_CAT_ARTIST, artist);
                            startActivity(in);

                        }
                    });
                }

            }
        }



        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            mFragment = new FreeFragment();
            ft.add(android.R.id.content, mFragment);
            ft.attach(mFragment);
        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            ft.remove(mFragment);
        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {

        }
    }
  • 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-13T21:34:06+00:00Added an answer on June 13, 2026 at 9:34 pm

    Use FreeFragment.getActivity() method to access your Fragment’s Activity, and after that get Context and SystemService

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

Sidebar

Related Questions

Can anyone please tell me how to extract this kind of data : [{number:8457215152,type:Cell,state:LA,country:US,tz:CT,zip:70546,msa:0},{number:4363685555,type:Cell,state:LA,country:US,tz:CT,zip:70546,msa:0}]
Can anyone please explain or suggest some good tutorial for the method of matrix
Can anyone please tell me what I should be using and why this method
Can anyone please tell/suggest me how can i store and retrieve comments multiple/different values
Can anyone please explain me the use of development certificates and how I can
Can anyone please explain me what delegateEvents in backbone.js does? The documentation did not
Can anyone please explain how to replace the values of one column with the
Can anyone please tell me how they accomplished disabling the bounce effect in safari
can anyone please tell me how can i retrieve data in jquery when i
Can anyone please tell me how to do a percent sign in objective C

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.