Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为成员变量“赋值构造函数参数”;。这不能从静态上下文中引用;_Java_Android - Fatal编程技术网

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(…)
是一个工厂方法,而不是构造函数!