尝试在使用V​​OLLEY库时调用null对象引用上的虚方法

时间:2015-11-19 13:17:09

标签: android android-volley android-networking

我正在使用Volley库来执行网络请求。我收到错误“Attemp在空对象引用上调用虚方法”。我无法解决此问题。

1。 FriendListActivity.java

public class FriendListActivity extends Activity {
    public static final String TAG = FriendListActivity.class.getSimpleName();
    private ListView listView;
    private FeedListFriendsAdapter listAdapter;
    private List<FeedItemFriends> feedItems;
    private String URL_FEED_FriendListActivity = "";
    AlmaChatDatabase almaChatDatabase;
    int uid;
    String accessToken, api;
    APIConfiguration apiConfiguration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friend_list);
        listView = (ListView) findViewById(R.id.listViewFriends);
        feedItems = new ArrayList<FeedItemFriends>();
        listAdapter = new FeedListFriendsAdapter(this, feedItems);
        listView.setAdapter(listAdapter);

        almaChatDatabase = new AlmaChatDatabase(this);
        uid = almaChatDatabase.getUserID();
        accessToken = almaChatDatabase.getAccessToken();

        apiConfiguration = new APIConfiguration();
        api = apiConfiguration.getApi();
        // /webservice/listMyFriends?user_id=<user_id>&access_token=<access_token>
        URL_FEED_FriendListActivity = api + "/webservice/listMyFriends?user_id=" + uid + "&access_token=" + accessToken;
        Log.e("URL", URL_FEED_FriendListActivity);
        Log.e("Network request", "Fresh Request");

        // making fresh volley request and getting json
        JsonObjectRequest jsonReq = new JsonObjectRequest(Request.Method.GET,
                URL_FEED_FriendListActivity, null, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                Log.e(TAG, "Response: " + response.toString());
                if (response != null) {
                    // parseJsonFeed(response);
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
            }
        });

        // Adding request to volley request queue
        AppController.getInstance().addToRequestQueue(jsonReq);
    }
}

2.FeedListFriendsAdapter.java

public class FeedListFriendsAdapter extends BaseAdapter {
    private Activity activity;
    private LayoutInflater inflator;
    private List<FeedItemFriends> feedItems;
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();

    public FeedListFriendsAdapter(Activity activity, List<FeedItemFriends> feedItems) {
        this.activity = activity;
        this.feedItems = feedItems;
    }

    @Override
    public int getCount() {
        return feedItems.size();
    }

    @Override
    public Object getItem(int position) {
        return feedItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (inflator == null)
            inflator = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflator.inflate(R.layout.feed_item_friend_list, parent, false);
        if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();

        NetworkImageView profile_pic_friend = (NetworkImageView) convertView.findViewById(R.id.profilePicFriends);
        TextView friend_name = (TextView) convertView.findViewById(R.id.friendsName);

        FeedItemFriends item = feedItems.get(position);
        profile_pic_friend.setImageUrl(item.getFriendsProfilePic(), imageLoader); // Profile pic of Friends
        friend_name.setText(item.getFriendsName());
        return convertView;
    }
}

第3。 logcat的

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.android.volley.toolbox.ImageLoader com.almabay.almachat.app.AppController.getImageLoader()' on a null object reference

0 个答案:

没有答案