我想从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};
//...
}
最佳答案
这是因为markerID
是null
。
检查您的以下行
//I want to get the markerID variable from pinboardActivity:
markerID = pinboard.markerID;
pinboard.markerID
返回一个空对象。