java - 将构造函数参数分配给成员变量, ".this cannot be referenced from a static context"

标签 java android

我有课,DFragment 。我正在通过 List<Map<String, String>>到它的构造函数,我需要将传入的值存储在成员变量 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;
    }

}

我做错了什么以及如何正确地将传入的值存储在我的成员变量中?

最佳答案

你可以试试这个,

fragment 类,

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;
    }

}

您可以按如下方式传递列表:

DFragment dialog = new DFragment (mylist);

关于java - 将构造函数参数分配给成员变量, ".this cannot be referenced from a static context",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29484865/

相关文章:

android - ViewPager + PagerAdapter 在前两项后显示空白页

java - Spring Boot Bootstrap 图标在 HTTPS 重新加载时不显示

java - Stream.parallel() 是否使用新线程?

java - 我可以在setcontentview android之前以编程方式创建ImageView吗

android - 'Start rollout to production' 按钮在 'Timed Publishing' 中会做什么

java - TextView 不会滚动

java - 找出 YearMonth 实例的最后一天(在 LocalDate 中) - joda 库

java - 将 JList 值从 JFrame 传递到 JDialog

java - jquery 将对象内部的对象发送到 Controller

Android模拟器sd卡突然只读(挂载失败?)