从 BaseAdapter 访问 Activity 的变量
Access Variable of Activity from BaseAdapter
我想从 BaseAdapter
class 中的 activity 访问一个变量,我用它设置了 activity 的 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
返回空对象。
我想从 BaseAdapter
class 中的 activity 访问一个变量,我用它设置了 activity 的 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
返回空对象。