adapter.notifyDataSetChanged 的 NullPointerException?
NullPointerException for adapter.notifyDataSetChanged?
我有一个 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();
看起来您从未初始化变量 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;
}
}
我有一个 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();
看起来您从未初始化变量 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;
}
}