Java 为成员变量“赋值构造函数参数”;。这不能从静态上下文中引用;
我有一个类,Java 为成员变量“赋值构造函数参数”;。这不能从静态上下文中引用;,java,android,Java,Android,我有一个类,DFragment。我正在将一个列表传递给它的构造函数,我需要将传入的值存储在成员变量subVitalList中 我试着这样做: this.subVitalList = subVitalList; 但我在该行收到以下错误: com.example.DFragment.this cannot be referenced from a static context 我不知道该怎么做。以下是DFragment的代码: public class DFragment extends Dia
DFragment
。我正在将一个列表
传递给它的构造函数,我需要将传入的值存储在成员变量subVitalList
中
我试着这样做:
this.subVitalList = subVitalList;
但我在该行收到以下错误:
com.example.DFragment.this cannot be referenced from a static context
我不知道该怎么做。以下是DFragment
的代码:
public class DFragment extends DialogFragment {
Context context;
List<Map<String, String>> subVitalList;
ListView vitalEntryListView;
LayoutInflater mInflater;
public static DFragment newInstance(List<Map<String, String>> subVitalList,int i) {
DFragment f = new DFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
//args.putInt("num", num);
args.putInt("num",i);
//List<List<String>> svl = getArguments().getStringArrayList(subVitalList);
return f;
}
}
公共类DfFragment扩展了DialogFragment{
语境;
列表子列表;
ListView VitalEntryList视图;
拉平机;
公共静态数据片段newInstance(列表子VitalList,int i){
DFragment f=新的DFragment();
//提供num输入作为参数。
Bundle args=新Bundle();
//args.putInt(“num”,num);
参数putInt(“num”,i);
//List svl=getArguments().getStringArrayList(subVitalList);
返回f;
}
}
我做错了什么?如何将传入的值正确地存储在成员变量中?您可以试试这个
片段类
public class DFragment extends DialogFragment {
List<Map<String, String>> mylist;
public DFragment () {
// Empty constructor required for DialogFragment
}
public DFragment (List<Map<String, String>> mylist) {
this.mylist= mylist;
}
}
你可以试试这个
片段类
public class DFragment extends DialogFragment {
List<Map<String, String>> mylist;
public DFragment () {
// Empty constructor required for DialogFragment
}
public DFragment (List<Map<String, String>> mylist) {
this.mylist= mylist;
}
}
你能解释一下“但它不起作用”吗?它说的是com.example.DFragment。这不能从静态上下文引用。你必须将
subVitalList
传递给参数,并在onCreateView
或其他片段的方法中获得它。您不能像this.subvitalist=subvitalist
因为newInstance
是static
方法,所以不能将非静态值设置为静态值。对不起,不清楚。如何在onCreateView中获取它?静态数据片段newInstance(…)
是一个工厂方法,而不是构造函数!你能解释一下“但它不起作用”吗?它说的是com.example.DFragment。这不能从静态上下文引用。你必须将subVitalList
传递给参数,并在onCreateView
或其他片段的方法中获得它。您不能像this.subvitalist=subvitalist
因为newInstance
是static
方法,所以不能将非静态值设置为静态值。对不起,不清楚。如何在onCreateView中获取它?静态数据片段newInstance(…)
是一个工厂方法,而不是构造函数!