1

I need public class FragmentB that extends ActionBarActivity. It has no errors, but when I try to call it in an adapter.

FragmentPagerAdapter 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;
    }
}

It says that I need a class that extends android.support.v4.app.Fragment at return b. At Fragment A and C there is no implementation of ActionBarActivity, that is why there is no error, while in return b there is.

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 ViewPager for sliding.

Fragment B code:

private static final String TAG = FragmentB.class.getSimpleName();

private static final String url = "http://server.si/events.php";
private ProgressDialog pDialog;
private List<Event> eventList = new ArrayList<Event>();
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<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
                    hidePDialog();

                    for (int i = 0; i < 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;
    }
}

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

    @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;
    }

This is the error at run:

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.<init>(CustomListAdapter.java:27)
            at teamtwo.event.com.events.FragmentB.onCreate(FragmentB.java:50)

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.

WinterChilly
  • 1,549
  • 3
  • 21
  • 34
  • 1
    `FragmentB that extends ActionBarActivity`, why?, it is Fragment or Activity? – Xcihnegn Mar 30 '15 at 08:47
  • Which is line 27 in `CustomListAdapter`? – Soana Mar 30 '15 at 09:10
  • @Xcihnegn It's a fragment, but when i did it without them i used ActionBarActivity, so i don't know what to extend to. @Soana `ImageLoader imageLoader = AppController.getInstance().getImageLoader(); ` – WinterChilly Mar 30 '15 at 11:53
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Soana Mar 30 '15 at 11:56
  • @Soana, so just change the extends of FragmentB to be `Fragment` – Xcihnegn Mar 30 '15 at 12:01
  • You should extends Fragment but be careful to use support.v4.fragment or android.fragment – Emre Aktürk Mar 30 '15 at 12:07
  • @EmreAktürk I'm already using android.support.v4.app.Fragment at fragmentB – WinterChilly Mar 30 '15 at 14:30
  • In ViewPager, next fragments are also being called. So when you are at Fragment A, Fragment B is also initilazed. That exception should be come from fragment which is not shown yet. The problem should be your imageview which is target of imageloader is not drawed. Thats the why it would be null. You may need to control it if it is null or not. – Emre Aktürk Apr 01 '15 at 06:27

1 Answers1

3

Add this line to your application tag in AndroidManifest.xml file inside MyApplication project:

android:name="primoz.test.com.volleytesting.AppController"

If you miss this line in application manifest, then AppController.getInstance() will be null each time you call it.

Amrut Bidri
  • 6,276
  • 6
  • 38
  • 80