Android 适配器的NullPointerException.notifyDataSetChanged?

Android 适配器的NullPointerException.notifyDataSetChanged?,android,android-adapter,android-recyclerview,Android,Android Adapter,Android Recyclerview,我有一个RecyclerView,它使我在适配器中崩溃。notifyDataSetChanged(): public class ListContent extends Activity { String _comment; public ArrayAdapter adapter; RecyclerView recList; Context context; @Override protected void onCreate(Bundle save

我有一个
RecyclerView
,它使我在
适配器中崩溃。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;
    }

看起来您从未初始化变量
适配器。所以它是
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;
    }
}
公共类ListContent扩展活动{
字符串注释;
公共联系人适配器;
回收视图重新列表;
语境;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u listcontent);
recList=(RecyclerView)findViewById(R.id.cardList);
recList.setHasFixedSize(真);
LinearLayoutManager llm=新的LinearLayoutManager(本);
llm.设置方向(线性布局管理器.垂直);
重新登录setLayoutManager(llm);

_comment=”“这是我的字符串,适配器初始化在哪里?因为您只是在通知它。初始化您的适配器,或者如果ca是您的适配器,则使用ca.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;
    }
}