adapter.notifyDataSetChanged的NullPointerException?

时间:2015-06-18 07:10:44

标签: android android-adapter android-recyclerview

我有一个RecyclerView让我崩溃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();

enter image description here

2 个答案:

答案 0 :(得分:1)

看起来你永远不会初始化变量adapter。所以它是null。这就是你获得NPE的原因。

答案 1 :(得分:0)

您在此处声明了适配器:

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