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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:31:28+00:00 2026-06-12T09:31:28+00:00

My tabs fragments doesnt seems to show anything. I know there must be something

  • 0

My tabs fragments doesnt seems to show anything. I know there must be something missing in my codes. Could someone please help me? Below are my attempt.

Modified after the suggestion by UgglyNoodle

Problems : My fragment Tab is not showing anything.

MainActivity.java

  public class MainActivity extends SherlockFragmentActivity implements TabListener {
    private Fragment mFragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.setDisplayShowTitleEnabled(true);

        //mFragment = new AppleFragment();

        Tab tab = actionBar.newTab()
                .setText("Apple")
                .setTabListener(this)
                .setIcon(R.drawable.apple);

        actionBar.addTab(tab);


    }

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

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

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

AppleFragment.java

    public class AppleFragment extends SherlockListFragment{


        static final String URL = "http://api.androidhive.info/pizza/?format=xml";
        // XML node keys
        static final String KEY_ITEM = "item"; // parent node
        static final String KEY_ID = "id";
        static final String KEY_NAME = "name";
        static final String KEY_COST = "cost";
        static final String KEY_DESC = "description";



        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

        return super.onCreateView(inflater, container, savedInstanceState);

        }


        public void onActivityCreated(Bundle savedInstanceState) {

            ArrayList<HashMap<String, String>> 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_ITEM);
            // 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_NAME, parser.getValue(e, KEY_NAME));
                map.put(KEY_COST, "Rs." + parser.getValue(e, KEY_COST));
                map.put(KEY_DESC, parser.getValue(e, KEY_DESC));

                // adding HashList to ArrayList
                menuItems.add(map);
            }

            ListAdapter adapter = new SimpleAdapter(getActivity(), menuItems,
                    R.layout.list_item,
                    new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] {
                            R.id.name, R.id.desciption, R.id.cost });


            setListAdapter(adapter);    


        }
}

XMLParser.java

public class XMLParser {

    // constructor
    public XMLParser() {

    }

    /**
     * Getting XML from URL making HTTP request
     * @param url string
     * */
    public String getXmlFromUrl(String url) {
        String xml = null;

        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            xml = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // return XML
        return xml;
    }

    /**
     * Getting XML DOM element
     * @param XML string
     * */
    public Document getDomElement(String xml){
        Document doc = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {

            DocumentBuilder db = dbf.newDocumentBuilder();

            InputSource is = new InputSource();
                is.setCharacterStream(new StringReader(xml));
                doc = db.parse(is); 

            } catch (ParserConfigurationException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            } catch (SAXException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            } catch (IOException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            }

            return doc;
    }

    /** Getting node value
      * @param elem element
      */
     public final String getElementValue( Node elem ) {
         Node child;
         if( elem != null){
             if (elem.hasChildNodes()){
                 for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
                     if( child.getNodeType() == Node.TEXT_NODE  ){
                         return child.getNodeValue();
                     }
                 }
             }
         }
         return "";
     }

     /**
      * Getting node value
      * @param Element node
      * @param key string
      * */
     public String getValue(Element item, String str) {     
            NodeList n = item.getElementsByTagName(str);        
            return this.getElementValue(n.item(0));
        }
}

Logcat :

10-05 16:24:30.031: E/AndroidRuntime(19244): FATAL EXCEPTION: main
10-05 16:24:30.031: E/AndroidRuntime(19244): java.lang.RuntimeException: Unable to start activity ComponentInfo{in.wptrafficanalyzer.actionbarsherlocknavtab/in.wptrafficanalyzer.actionbarsherlocknavtab.MainActivity}: android.os.NetworkOnMainThreadException
10-05 16:24:30.031: E/AndroidRuntime(19244):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at android.os.Looper.loop(Looper.java:137)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at android.app.ActivityThread.main(ActivityThread.java:4512)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at java.lang.reflect.Method.invokeNative(Native Method)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at java.lang.reflect.Method.invoke(Method.java:511)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at dalvik.system.NativeStart.main(Native Method)
10-05 16:24:30.031: E/AndroidRuntime(19244): Caused by: android.os.NetworkOnMainThreadException
10-05 16:24:30.031: E/AndroidRuntime(19244):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at java.net.InetAddress.getAllByName(InetAddress.java:220)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at in.wptrafficanalyzer.actionbarsherlocknavtab.XMLParser.getXmlFromUrl(XMLParser.java:45)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at in.wptrafficanalyzer.actionbarsherlocknavtab.AppleFragment.onCreate(AppleFragment.java:50)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:834)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:505)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1136)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at android.app.Activity.performStart(Activity.java:4475)
10-05 16:24:30.031: E/AndroidRuntime(19244):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1943)
10-05 16:24:30.031: E/AndroidRuntime(19244):    ... 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-12T09:31:29+00:00Added an answer on June 12, 2026 at 9:31 am

    You don’t need your AppleFragment class. Instead, you can have your MainActivity to implement TabListener. Here is some quick code which gives you the basic idea, but I have not tested it and it will need some fixing.

    public class MainActivity extends SherlockFragmentActivity implements TabListener {
        private Fragment mFragment;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.main);
    
            ActionBar actionBar = getSupportActionBar();
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            actionBar.setDisplayShowTitleEnabled(true);
    
            Tab tab = actionBar.newTab()
                    .setText("Apple")
                    .setTabListener(this)
                    .setIcon(R.drawable.apple);
    
            actionBar.addTab(tab);
        }
    
        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
        if (mFragment == null) {
            mFragment = new ArrayListFragment();
            ft.add(R.id.content, mFragment, "some_tag");
        } else {
            ft.attach(mFragment);
        }
    
        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            ft.detach(mFragment);
        }
    
        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
        }
    }
    

    When you have multiple tabs, you will need to keep track of the multiple fragments, and attach and detach the right ones according to the tab selected.

    Finally, your ArrayListFragment should extend ListFragment, and you will still be able to use setListAdapter() and so on.

    This page has a pretty good example, although it is a little more complicated than what I have suggested.

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

Sidebar

Related Questions

Could someone please check on codes. My XMLParser has caught an exception. I'm trying
I am using Android Fragments with a viewpager setup. Basically there are 5 tabs
I am making an app which contains fragments within tabs.In this whenever I try
I have an app that uses the ActionBar with tabs in combination with Fragments.
I'm using ActionBarSherlock and have a little problem. In the Fragment Tabs example, there
I have a fragment interface with tabs along the bottom which open different fragments
I have 3 fragments and an activity. I want to enable tabs on the
Ok I have an app that implements tabs in the action bar using fragments.
I have two tabs hosted in my Main Activity (I use ActionBarSherlock). My fragments
I've been going through the ABS Fragments demo, and noticed that the tabs look

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.