我需要扩展public class FragmentBActionBarActivity。它没有错误,但是当我尝试在适配器中调用它时。

FragmentPagerAdapter代码:

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


它说我需要一个在android.support.v4.app.Fragment处扩展return b的类。在片段A和C中没有ActionBarActivity的实现,这就是为什么没有错误而在return b中没有错误的原因。

基本上,我需要显示片段B上的所有事件。如果我在没有片段的情况下对其进行测试,则可以使用它,但是使用它们会变成一个小问题。我需要片段,因为ViewPager可以滑动。

片段B代码:

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代码:
    公共类CustomListAdapter扩展BaseAdapter {
        私人活动活动;
        专用LayoutInflater充气机;
        私有列表eventItems;
        ImageLoader imageLoader = AppController.getInstance()。getImageLoader();
        公共CustomListAdapter(活动活动,列出eventItems){
            this.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;
    }


这是运行时的错误:

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)


看起来,我在使用OnCreate方法做错了。我不知道我应该将代码放在哪里。OnCreate在一切膨胀或创建活动和片段之前,而OnCreateView设置片段布局。我不知道该怎么做。

最佳答案

将此行添加到application中的AndroidManifest.xml标记中
 MyApplication项目中的文件:

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


如果您错过了应用程序清单中的这一行,则每次调用AppController.getInstance()都将为null。

10-08 07:05