我想从BaseAdapter类中的活动访问变量,并通过该变量设置活动的ListView
但是我总是会收到这个错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Integer.toString()' on a null object reference


这些是我的代码的相关部分:

public class GetAllEntrysListViewAdapter extends BaseAdapter {

Integer markerID;
PinboardActivity pinboard = new PinboardActivity();

public GetAllEntrysListViewAdapter(JSONArray jsonArray, Context context) {
    this.dataArray = jsonArray;
    this.context= context;
    //I want to get the markerID variable from pinboardActivity:
    markerID = pinboard.markerID;
    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
//...
public Cursor getLikes(DbHelper dbh) {
    //The following line is the one the error occurs
    String args[] = {markerID.toString(), pos};
    //...
}

最佳答案

这是因为markerIDnull
检查您的以下行

//I want to get the markerID variable from pinboardActivity:
    markerID = pinboard.markerID;


pinboard.markerID返回一个空对象。

07-28 12:30