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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:34:04+00:00 2026-06-14T01:34:04+00:00

I’m trying to implemention Check Internet Connection to my fragments tab. It doesnt seem

  • 0

I’m trying to implemention Check Internet Connection to my fragments tab. It doesnt seem to run. Can anyone please check on my codes. I want my app to check for internet connection before the fragment tabs are loaded.

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;
    Context appContext;
    private Fragment mFragment;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {

        checkInternetConnection();

        super.onActivityCreated(savedInstanceState);
    }




    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);

                    }
                });
            }

        }
    }

    private boolean checkInternetConnection() {
        ConnectivityManager cm = (ConnectivityManager) appContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        // test for connection
        if (cm.getActiveNetworkInfo() != null
                && cm.getActiveNetworkInfo().isAvailable()
                && cm.getActiveNetworkInfo().isConnected()) {
            new loadListView().execute();
            return true;
        } else {
            Toast.makeText(appContext, "Internet Connection Required" , Toast.LENGTH_LONG).show();


            AlertDialog.Builder builder = new AlertDialog.Builder(appContext);
             builder.setMessage("Internet Connection Required")
                    .setCancelable(false)
                    .setPositiveButton("Exit", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            System.exit(0);
                        }

                       });
            AlertDialog alert = builder.create();
             alert.show(); 

                 }

        return false;
     }

    @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) {

    }
}

LOGCAT :

11-01 17:06:30.585: E/AndroidRuntime(28090): FATAL EXCEPTION: main
11-01 17:06:30.585: E/AndroidRuntime(28090): java.lang.RuntimeException: Unable to start activity ComponentInfo{in.wptrafficanalyzer.actionbarsherlocknavtab/in.wptrafficanalyzer.actionbarsherlocknavtab.MainActivity}: java.lang.NullPointerException
11-01 17:06:30.585: E/AndroidRuntime(28090):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
11-01 17:06:30.585: E/AndroidRuntime(28090):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
11-01 17:06:30.585: E/AndroidRuntime(28090):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-01 17:06:30.585: E/AndroidRuntime(28090):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
11-01 17:06:30.585: E/AndroidRuntime(28090):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-01 17:06:30.585: E/AndroidRuntime(28090):    at android.os.Looper.loop(Looper.java:130)
11-01 17:06:30.585: E/AndroidRuntime(28090):    at android.app.ActivityThread.main(ActivityThread.java:3691)
11-01 17:06:30.585: E/AndroidRuntime(28090):    at java.lang.reflect.Method.invokeNative(Native Method)
11-01 17:06:30.585: E/AndroidRuntime(28090):    at java.lang.reflect.Method.invoke(Method.java:507)
11-01 17:06:30.585: E/AndroidRuntime(28090):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
11-01 17:06:30.585: E/AndroidRuntime(28090):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
11-01 17:06:30.585: E/AndroidRuntime(28090):    at dalvik.system.NativeStart.main(Native Method)
11-01 17:06:30.585: E/AndroidRuntime(28090): Caused by: java.lang.NullPointerException
11-01 17:06:30.585: E/AndroidRuntime(28090):    at in.wptrafficanalyzer.actionbarsherlocknavtab.FreeFragment.checkInternetConnection(FreeFragment.java:151)
11-01 17:06:30.585: E/AndroidRuntime(28090):    at in.wptrafficanalyzer.actionbarsherlocknavtab.FreeFragment.onActivityCreated(FreeFragment.java:51)
11-01 17:06:30.585: E/AndroidRuntime(28090):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:891)
11-01 17:06:30.585: E/AndroidRuntime(28090):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
11-01 17:06:30.585: E/AndroidRuntime(28090):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
11-01 17:06:30.585: E/AndroidRuntime(28090):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
11-01 17:06:30.585: E/AndroidRuntime(28090):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:505)
11-01 17:06:30.585: E/AndroidRuntime(28090):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
11-01 17:06:30.585: E/AndroidRuntime(28090):    at android.app.Activity.performStart(Activity.java:3817)
11-01 17:06:30.585: E/AndroidRuntime(28090):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1624)
11-01 17:06:30.585: E/AndroidRuntime(28090):    ... 11 more
  • 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-14T01:34:05+00:00Added an answer on June 14, 2026 at 1:34 am

    The problem in your xml.

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

    Fix this code and it should works.

    // check internet connection
    public class MainActivity extends FragmentActivity {
    
        Fragment1 fragment1 = new Fragment1();
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            if(!checkInternet()){
                // show error View
            }
            setContentView(R.layout.activity_main);     
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group

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.