Andriod Listview inside fragments throwing network exception
<p>I am trying display rss feed into a fragment with list view. I was able to generate list view from rssfeed but seeing issue when I click on any list view element to show link details in another activity. Can somebody can help me in resolving below issue</p>

<p>Below is stack trace</p>

<p>05-26 01:27:19.137: E/AndroidRuntime(719): FATAL EXCEPTION: main
05-26 01:27:19.137: E/AndroidRuntime(719): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.watchrss/com.watchrss.URLActivity}: android.os.NetworkOnMainThreadException
05-26 01:27:19.137: E/AndroidRuntime(719):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
05-26 01:27:19.137: E/AndroidRuntime(719):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
05-26 01:27:19.137: E/AndroidRuntime(719):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
05-26 01:27:19.137: E/AndroidRuntime(719):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
05-26 01:27:19.137: E/AndroidRuntime(719):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-26 01:27:19.137: E/AndroidRuntime(719):  at android.os.Looper.loop(Looper.java:137)
05-26 01:27:19.137: E/AndroidRuntime(719):  at android.app.ActivityThread.main(ActivityThread.java:4745)
05-26 01:27:19.137: E/AndroidRuntime(719):  at java.lang.reflect.Method.invokeNative(Native Method)
05-26 01:27:19.137: E/AndroidRuntime(719):  at java.lang.reflect.Method.invoke(Method.java:511)
05-26 01:27:19.137: E/AndroidRuntime(719):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-26 01:27:19.137: E/AndroidRuntime(719):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-26 01:27:19.137: E/AndroidRuntime(719):  at dalvik.system.NativeStart.main(Native Method)
05-26 01:27:19.137: E/AndroidRuntime(719): Caused by: android.os.NetworkOnMainThreadException
05-26 01:27:19.137: E/AndroidRuntime(719):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
05-26 01:27:19.137: E/AndroidRuntime(719):  at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
05-26 01:27:19.137: E/AndroidRuntime(719):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
05-26 01:27:19.137: E/AndroidRuntime(719):  at java.net.InetAddress.getAllByName(InetAddress.java:214)
05-26 01:27:19.137: E/AndroidRuntime(719):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
05-26 01:27:19.137: E/AndroidRuntime(719):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-26 01:27:19.137: E/AndroidRuntime(719):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-26 01:27:19.137: E/AndroidRuntime(719):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
05-26 01:27:19.137: E/AndroidRuntime(719):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-26 01:27:19.137: E/AndroidRuntime(719):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-26 01:27:19.137: E/AndroidRuntime(719):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-26 01:27:19.137: E/AndroidRuntime(719):  at com.watchrss.URLActivity.getHtml(URLActivity.java:87)
05-26 01:27:19.137: E/AndroidRuntime(719):  at com.watchrss.URLActivity.onCreate(URLActivity.java:43)
05-26 01:27:19.137: E/AndroidRuntime(719):  at android.app.Activity.performCreate(Activity.java:5008)
05-26 01:27:19.137: E/AndroidRuntime(719):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
05-26 01:27:19.137: E/AndroidRuntime(719):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
05-26 01:27:19.137: E/AndroidRuntime(719):  ... 11 more</p>

<p>My code is as shown below   </p>

<pre><code>import java.util.List;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.rssfeed.adapter.RssReaderListAdapter;
import com.rssfeed.helper.RssFeedStructure;
import com.rssfeed.helper.XmlHandler;

public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener {

    private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";

    private static final String FEED_TOP_STORIES               = "testurl"; 
    private static final String REAL_HEAD_LINES                = "testurl2";


    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


        // For each of the sections in the app, add a tab to the action bar.
        actionBar.addTab(actionBar.newTab().setText(R.string.title_section1)
                .setTabListener(this));
        actionBar.setBackgroundDrawable(getWallpaper());
        //actionBar.setIcon(R.drawable.ic_launcher);
        actionBar.addTab(actionBar.newTab().setText(R.string.title_section2)
                .setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText(R.string.title_section3)
                .setTabListener(this));
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
            getActionBar().setSelectedNavigationItem(
                    savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar()
                .getSelectedNavigationIndex());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, show the tab contents in the
        // container

        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER,
                tab.getPosition() + 1);
        fragment.setArguments(args);
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.container, fragment).commit();
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
        // fragmentTransaction.
    }

    /**
     * A dummy fragment representing a section of the app, but that simply
     * displays dummy text.
     */
    public static class DummySectionFragment extends Fragment {

        ListView _rssFeedListView = null;
        TextView _rssFeedtextView = null;

        List&lt;RssFeedStructure&gt; rssStr = null;   
        View view = null;
        View view1 = null;

        public DummySectionFragment() {
        }

        public static final String ARG_SECTION_NUMBER = "section_number";

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

            Bundle args = getArguments();

            view = inflater.inflate(R.layout.rssfeedreaderactivity,container, false);
            _rssFeedListView = (ListView) view.findViewById(R.id.listV_main);

            view1 = inflater.inflate(R.layout.urllayout,container, false);
            _rssFeedtextView = (TextView) view1.findViewById(R.id.textView1); 

            RssFeedTask rssTask = new RssFeedTask();

            switch (args.getInt(ARG_SECTION_NUMBER)) {

                case 1:

                    rssTask.execute(FEED_TOP_STORIES);

                      _rssFeedListView.setOnItemClickListener(new OnItemClickListener() {
                              public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
                                    Object o = _rssFeedListView.getItemAtPosition(position);

                                RssFeedStructure obj_itemDetails = (RssFeedStructure)o;
                                String objURL=obj_itemDetails.getUrl();

                                Intent intent = new Intent(getActivity(),URLActivity.class);
                                intent.putExtra("OBJURL", objURL);

                                startActivity(intent);

                                Toast.makeText(getActivity(), "You have chosen : "+ obj_itemDetails.getUrl().toString(), Toast.LENGTH_LONG).show();
                              }
                            });

                    break;

                case 2:
                    rssTask.execute(REAL_HEAD_LINES);               
                    break;

                case 3:
                    Toast.makeText(getActivity(), "Fragement Three Action",Toast.LENGTH_LONG).show();
                    break;
            }

            return view;

        }

          public interface OnItemSelectedListener {
                public void onRssItemSelected(String link);
              }
        private class RssFeedTask extends AsyncTask&lt;String, Void, String&gt; {
            // private String Content;
            private ProgressDialog Dialog;
            String response = "";

            @Override
            protected void onPreExecute() {
                Dialog = new ProgressDialog(getActivity());
                Dialog.setMessage("Loading...");
                Dialog.show();

            }

            @Override
            protected String doInBackground(String... urls) {
          try {



              XmlHandler rh = new XmlHandler();
              rssStr = rh.getLatestArticles(urls[0]);  


                } catch (Exception e) {
                }
                return response;

            }

            @Override
            protected void onPostExecute(String result) {

                  if(rssStr != null){
                    ListAdapter _adapter = new RssReaderListAdapter(getActivity(),rssStr);
                    _rssFeedListView.setAdapter(_adapter);


                  }

                    Dialog.dismiss();
            }
        }

    }
}
</code></pre>
