Android, ViewPage Indictator and listview from json
<p>My knowledge of Java coding is very limited, about 2 days worth in fact. I agreed to try do this project for a charity i volunteer for. I have everything else sorted but struggling with this. </p>

<p>The ultimate aim for the project is to have a tabbed interface each with a separate listview that populates data from a json source. So far i have sourced some nice code by Jake Wharton (ViewPagerIndicator). Ive edited the sample to leave the test fragment which compliles and gives me the interface i want, i can display a listview within the tab but because its a json feed i can't run it in the main thread and it's getting awfy confusing for me when trying to use asynctask.</p>

<p>Any constructive input, directions to some good tutorial i could learn to do this from would be greatly appreciated. If anyone could go as far as fixing it to make it work we would be more than happy to link back to your website and thank you in the supporters site of our website.</p>

<p>Thanks</p>

<p>ps. the final app will provide data to communities in Africa on a whole host of things, mainly vital farming data to help grow their crops. </p>

<p>The code is as follows so far.</p>

<pre><code>public final class TestFragment extends Fragment {
    private static final String KEY_CONTENT = "TestFragment:Content";
    SimpleAdapter adapter;
    public static TestFragment newInstance(String content) {
        TestFragment fragment = new TestFragment();

        StringBuilder builder = new StringBuilder();
        for (int i = 0; i &lt; 20; i++) {
            builder.append(content).append(" ");
        }
        builder.deleteCharAt(builder.length() - 1);
        fragment.mContent = builder.toString();

        return fragment;
    }

    private String mContent = "???";

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

        if ((savedInstanceState != null) &amp;&amp; savedInstanceState.containsKey(KEY_CONTENT)) {
            mContent = savedInstanceState.getString(KEY_CONTENT);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //ListView v = new ListView(getActivity());
    // i thought i could call the asynctask task from here and populate the listview

       LinearLayout layout = new LinearLayout(getActivity());
       layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
       layout.setGravity(Gravity.CENTER);
       layout.addView(v);

        return layout;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(KEY_CONTENT, mContent);
    }
}
</code></pre>

<p>And here is the code from my test to fetch the json data.</p>

<pre><code>JSONObject json = JSONfunctions.getJSONfromURL("url_to_json_data.com");

        try{
            JSONArray  thelist = json.getJSONArray("mainlist");

            for(int i=0;i&lt;thelist.length();i++){                        
                HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();    
                JSONObject e = thelist.getJSONObject(i);            
                map.put("id",   e.getString("id"));
                    map.put("Item1", e.getString("Item1"));
                map.put("Item2", e.getString("Item2"));
                    mylist.add(map);            
            }       
        }catch(JSONException e)        {
             Log.e("log_tag", "Error parsing data "+e.toString());
        }    

        final ListView lv = getListView();


        ///im guessing these will have to be changed as well for the layout, point to simple_tabs.xml ?
        adapter = new SimpleAdapter(this, mylist , R.layout.main, 
                        new String[] { "Item1", "Item2" }, 
                        new int[] { R.id.item_title });  
        setListAdapter(adapter);
</code></pre>

<p>Here was my attempt.</p>

<pre><code>public final class TestFragment extends Fragment {
    private static final String KEY_CONTENT = "TestFragment:Content";
    SimpleAdapter adapter;
    ListView v = new ListView(getActivity());
      ArrayList&lt;HashMap&lt;String, String&gt;&gt; mylist = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
    public static TestFragment newInstance(String content) {
        TestFragment fragment = new TestFragment();

        StringBuilder builder = new StringBuilder();
        for (int i = 0; i &lt; 20; i++) {
            builder.append(content).append(" ");
        }
        builder.deleteCharAt(builder.length() - 1);
        fragment.mContent = builder.toString();

        return fragment;
    }

    private String mContent = "???";

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

        if ((savedInstanceState != null) &amp;&amp; savedInstanceState.containsKey(KEY_CONTENT)) {
            mContent = savedInstanceState.getString(KEY_CONTENT);
        }
    }

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

    // i thought i could call the asynctask task from here and populate the listview
       new GetTheData().execute();
       LinearLayout layout = new LinearLayout(getActivity());
       layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
       layout.setGravity(Gravity.CENTER);
       layout.addView(v);

        return layout;
    }





    public class GetTheData extends AsyncTask&lt;Void, Void, Void&gt; {

        @Override
        protected Void doInBackground(Void... params) {    

               JSONObject json = JSONfunctions.getJSONfromURL("the url");

            try{
                    JSONArray  thelist = json.getJSONArray("mainlist");

                    for(int i=0;i&lt;thelist.length();i++){                        
                        HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();    
                        JSONObject e = thelist.getJSONObject(i);            
                        map.put("id",   e.getString("id"));
                        map.put("Item1", e.getString("Item1"));
                        mylist.add(map);            
                    }       
                 }catch(JSONException e)        {
                     Log.e("log_tag", "Error parsing data "+e.toString());
                 }    



            return null;
        }

        protected void onProgressUpdate(Integer... progress) {
          //  setProgressPercent(progress[0]);
        }

        protected void onPostExecute(Long result) {
             adapter = new SimpleAdapter( getActivity(), mylist,
                        android.R.layout.simple_list_item_1, new String[] { "Item1", "Item2" }, 
                        new int[] { R.id.item_title } );
                    v.setAdapter( adapter );    
        }
    }
</code></pre>

<p>and here is the error log from my attempt.</p>

<pre><code>: FATAL EXCEPTION: main
: java.lang.NullPointerException
:   at android.view.ViewConfiguration.get(ViewConfiguration.java:332)
:   at android.view.View.&lt;init&gt;(View.java:3236)
:   at android.view.View.&lt;init&gt;(View.java:3295)
:   at android.view.ViewGroup.&lt;init&gt;(ViewGroup.java:427)
:   at android.widget.AdapterView.&lt;init&gt;(AdapterView.java:235)
:   at android.widget.AbsListView.&lt;init&gt;(AbsListView.java:758)
:   at android.widget.ListView.&lt;init&gt;(ListView.java:141)
:   at android.widget.ListView.&lt;init&gt;(ListView.java:137)
:   at android.widget.ListView.&lt;init&gt;(ListView.java:133)
:   at com.viewpagerindicator.sample.TestFragment.&lt;init&gt;(TestFragment.java:28)
:   at com.viewpagerindicator.sample.TestFragment.newInstance(TestFragment.java:31)
:   at com.viewpagerindicator.sample.SampleTabsDefault$GoogleMusicAdapter.getItem(SampleTabsDefault.java:58)
:   at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:95)
:   at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:649)
:   at android.support.v4.view.ViewPager.populate(ViewPager.java:783)
:   at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:433)
:   at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:405)
:   at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:386)
:   at com.viewpagerindicator.TabPageIndicator.setCurrentItem(TabPageIndicator.java:238)
:   at com.viewpagerindicator.TabPageIndicator.onMeasure(TabPageIndicator.java:116)
:   at android.view.View.measure(View.java:15172)
:   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
:   at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
:   at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
:   at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
:   at android.view.View.measure(View.java:15172)
:   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
:   at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
:   at android.view.View.measure(View.java:15172)
:   at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
:   at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
:   at android.view.View.measure(View.java:15172)
:   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
:   at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
:   at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
:   at android.view.View.measure(View.java:15172)
:   at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
:   at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
:   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
:   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
:   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
:   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
:   at android.view.Choreographer.doCallbacks(Choreographer.java:555)
:   at android.view.Choreographer.doFrame(Choreographer.java:525)
:   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
:   at android.os.Handler.handleCallback(Handler.java:615)
:   at android.os.Handler.dispatchMessage(Handler.java:92)
:   at android.os.Looper.loop(Looper.java:137)
:   at android.app.ActivityThread.main(ActivityThread.java:4745)
:   at java.lang.reflect.Method.invokeNative(Native Method)
:   at java.lang.reflect.Method.invoke(Method.java:511)
:   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
:   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
:   at dalvik.system.NativeStart.main(Native 
</code></pre>
