title: Fragment ViewPager with CustomListAdapter and Volley?
body: <p>I need <code>public class FragmentB</code> that extends <code>ActionBarActivity</code>. It has no errors, but when I try to call it in an adapter.</p>
  
  <p>FragmentPagerAdapter code: </p>
  
  <pre><code>public class FragmentPagerAdapter extends android.support.v4.app.FragmentPagerAdapter{
  
      private static int NUM_ITEMS = 3;
      public FragmentPagerAdapter(FragmentManager fragmentManager) {
          super(fragmentManager);
      }
  
      // Returns total number of pages
      @Override
      public int getCount() {
          return NUM_ITEMS;
      }
      @Override
      public Fragment getItem(int position) {
          switch (position) {
              case 0:
                  FragmentA a = new FragmentA();
                  return a;
              case 1:
                  FragmentB b = new FragmentB();
                  return b;
              case 2:
                  FragmentC c = new FragmentC();
                  return c;
              default:
                  return null;
          }
      }
  
      // Returns the page title for the top indicator
      @Override
      public CharSequence getPageTitle(int position) {
          return "Page " + position;
      }
  }
  </code></pre>
  
  <p>It says that I need a class that extends <code>android.support.v4.app.Fragment</code> at <code>return b</code>. At Fragment A and C there is no implementation of <code>ActionBarActivity</code>, that is why there is no error, while in <code>return b</code> there is.</p>
  
  <p>Basicly I need to show all the events on Fragment B. If I test it without the fragments it works, but with them it becomes a slight problem. I need the fragments because of the <code>ViewPager</code> for sliding.</p>
  
  <p>Fragment B code:</p>
  
  <pre><code>private static final String TAG = FragmentB.class.getSimpleName();
  
  private static final String url = "http://server.si/events.php";
  private ProgressDialog pDialog;
  private List&lt;Event&gt; eventList = new ArrayList&lt;Event&gt;();
  private ListView listView;
  private CustomListAdapter adapter;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      adapter = new CustomListAdapter(getActivity(), eventList);
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      View rootView = inflater.inflate(R.layout.fragment_b, container, false);
  
      ListView listView = (ListView) rootView.findViewById(R.id.list);
      listView.setAdapter(adapter);
  
      pDialog.setMessage("Loading Events...");
      pDialog.show();
  
      JsonArrayRequest eventReq = new JsonArrayRequest(url,
              new Response.Listener&lt;JSONArray&gt;() {
                  @Override
                  public void onResponse(JSONArray response) {
                      Log.d(TAG, response.toString());
                      hidePDialog();
  
                      for (int i = 0; i &lt; response.length(); i++) {
                          try {
                              JSONObject obj = response.getJSONObject(i);
                              Event event = new Event();
                              event.setTitle(obj.getString("name"));
                              event.setPicture_url(obj.getString("picture_url"));
                              event.setCity(obj.getString("city"));
                              event.setDate(obj.getString("date"));
  
                              eventList.add(event);
  
                          } catch (JSONException e) {
                              e.printStackTrace();
                          }
                      }
  
                      // notifying list adapter about data changes
                      // so that it renders the list view with updated data
                      adapter.notifyDataSetChanged();
                  }
              }, new Response.ErrorListener() {
                  @Override
                  public void onErrorResponse(VolleyError error) {
                      VolleyLog.d(TAG, "Error: " + error.getMessage());
                      hidePDialog();
  
                  }
      });
  
      AppController.getInstance().addToRequestQueue(eventReq);
  
      return rootView;
  }
  
  @Override
  public void onDestroy() {
      super.onDestroy();
      hidePDialog();
  }
  
  private void hidePDialog() {
      if (pDialog != null) {
          pDialog.dismiss();
          pDialog = null;
      }
  }
  </code></pre>
  
  <p>CustomListAdapter code:
      public class CustomListAdapter extends BaseAdapter {
          private Activity activity;
          private LayoutInflater inflater;
          private List eventItems;
          ImageLoader imageLoader = AppController.getInstance().getImageLoader();
          public CustomListAdapter(Activity activity, List eventItems) {
              this.activity = activity;
              this.eventItems = eventItems;
          }</p>
  
  <pre><code>    @Override
      public int getCount() {
          return eventItems.size();
      }
  
      @Override
      public Object getItem(int location) {
          return eventItems.get(location);
      }
  
      @Override
      public long getItemId(int position) {
          return position;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          if (inflater == null)
              inflater = (LayoutInflater) activity
                      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          if (convertView == null)
              convertView = inflater.inflate(R.layout.list_row, null);
  
          if (imageLoader == null)
              imageLoader = AppController.getInstance().getImageLoader();
          NetworkImageView thumbNail = (NetworkImageView) convertView
                  .findViewById(R.id.picture);
          TextView name = (TextView) convertView.findViewById(R.id.name);
          TextView city = (TextView) convertView.findViewById(R.id.city);
          TextView date = (TextView) convertView.findViewById(R.id.date);
  
          Event m = eventItems.get(position);
  
          thumbNail.setImageUrl(m.getPicture_url(), imageLoader);
  
          name.setText(m.getTitle());
  
          city.setText("City: " + String.valueOf(m.getCity()));
  
          date.setText(String.valueOf(m.getDate()));
  
          return convertView;
      }
  </code></pre>
  
  <p>This is the error at run: </p>
  
  <pre><code>java.lang.NullPointerException: Attempt to invoke virtual method 'com.android.volley.toolbox.ImageLoader teamtwo.event.com.events.Events.AppController.getImageLoader()' on a null object reference
              at teamtwo.event.com.events.Events.CustomListAdapter.&lt;init&gt;(CustomListAdapter.java:27)
              at teamtwo.event.com.events.FragmentB.onCreate(FragmentB.java:50)
  </code></pre>
  
  <p>Looks like, I'm doing something wrong with OnCreate methods. I don't know where should i put the code in. OnCreate is before everything inflates or  creates both the activity and the fragment, while OnCreateView sets the fragment layout. I have no idea what to do.</p>