繁体   English   中英

适配器的 NullPointerException.notifyDataSetChanged?

[英]NullPointerException for adapter.notifyDataSetChanged?

我有一个RecyclerView ,它让我在adapter.notifyDataSetChanged();崩溃adapter.notifyDataSetChanged();

public class ListContent extends Activity {
    String _comment;
    public ArrayAdapter adapter;
    RecyclerView recList;
    Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listcontent);
                recList = (RecyclerView) findViewById(R.id.cardList);
        recList.setHasFixedSize(true);
        LinearLayoutManager llm = new LinearLayoutManager(this);
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        recList.setLayoutManager(llm);
        _comment = "" here is my string <===
        ContactAdapter ca = new ContactAdapter(createList(_comment),context);
        recList.setAdapter(ca);
        }

    private List<Struct_ListContent> createList(String comment) {


        List<Struct_ListContent> result = new ArrayList<Struct_ListContent>();
        Pattern p = Pattern.compile("<span style=\"color: rgb\\(51, 102, 255\\);\">([^<]*)</span>", Pattern.MULTILINE | Pattern.DOTALL);
        for (Matcher m = p.matcher(comment); m.find(); ) {
            Struct_ListContent st_list = new Struct_ListContent();
            st_list.Title_ = m.group(1);
            result.add(st_list);
        }
        adapter.notifyDataSetChanged();
    return  result;
    }

让我在这一行崩溃:

adapter.notifyDataSetChanged();

在此处输入图片说明

看起来您从未初始化变量adapter 所以它是null 这就是您获得 NPE 的原因。

您在此处声明了您的适配器:

public ArrayAdapter adapter;

然后在您的 onCreate() 中,您创建了另一个适配器

ContactAdapter ca = new ContactAdapter(createList(_comment),context);
recList.setAdapter(ca);

所以当你调用adapter.notifyDataSetChanged(); ,您的适配器尚未初始化。

试试下面的代码

public class ListContent extends Activity {
    String _comment;
    public ContactAdapter adapter;
    RecyclerView recList;
    Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listcontent);
                recList = (RecyclerView) findViewById(R.id.cardList);
        recList.setHasFixedSize(true);
        LinearLayoutManager llm = new LinearLayoutManager(this);
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        recList.setLayoutManager(llm);
        _comment = "" here is my string <===
        adapter = new ContactAdapter(createList(_comment),context);
        recList.setAdapter(adapter);
        }

    private List<Struct_ListContent> createList(String comment) {


        List<Struct_ListContent> result = new ArrayList<Struct_ListContent>();
        Pattern p = Pattern.compile("<span style=\"color: rgb\\(51, 102, 255\\);\">([^<]*)</span>", Pattern.MULTILINE | Pattern.DOTALL);
        for (Matcher m = p.matcher(comment); m.find(); ) {
            Struct_ListContent st_list = new Struct_ListContent();
            st_list.Title_ = m.group(1);
            result.add(st_list);
        }
        adapter.notifyDataSetChanged();
    return  result;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM