齐射RequestQueue NullPointerException

时间:2019-03-18 04:44:52

标签: java nullpointerexception android-volley

调试器:

java.lang.NullPointerException: Attempt to invoke virtual method 'com.android.volley.Request com.android.volley.RequestQueue.add(com.android.volley.Request)' on a null object reference
    at .MainActivity.addRemove(MainActivity.java:234)
    at .ListItemAdapter$2.onClick(ListItemAdapter.java:66)

问题出在哪里?

public void addRemove(Integer pos, List<ListItem> listItems) {
    if (pos!=null){
    boolean isSelected=listItems.get(pos).getIsSelected();
    final String spotId=listItems.get(pos).getId();
    if (isSelected==true){
    URL_D=URL_DATA+"api-spot-favorites-add";
    }
    else{
    URL_D=URL_DATA+"api-spot-favorites-remove";
    }
    StringRequest addRequest = new StringRequest(Request.Method.POST, URL_D, 

   ........    

    };

        requestQueue.add(addRequest);
}

还调用了addRemove(pos,listItems) ListItemAdapter类,它是用MainActivity类编写的

2 个答案:

答案 0 :(得分:0)

您是否已将AppController添加到AndroidManifest.xml? 如果没有,请添加

<application
    android:name=".AppController"
    android:allowBackup="true"
    android:icon="@mipmap/ic_laucher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_laucher_circle"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

答案 1 :(得分:0)

解决了问题。

移动了 public void addRemove(Integer pos,List listItems){} 到我在其中调用它的ListItemAdapter类,而不是从MainActivity类中调用它