I have a listView which need a customized adapter- Dict_Adapter. One of the constructor of Dict_Adapter is:
public Dict_Adapter(Context c,ArrayList<String> Meanings){
context=c;
meaning=Meanings;
type=2;
}
In getView of the adapter, the app will set two TextView and one ImageView like below:
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view==null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.dict_listview_items, null);
}
TextView Difficulty=(TextView) view.findViewById(R.id.Dict_ListView_Difficulty);
TextView Word=(TextView) view.findViewById(R.id.Dict_ListView_Word);
ImageView Arrow=(ImageView) view.findViewById(R.id.Dict_ListView_Arrow);
if (type==2){
Difficulty.setText(difficulty.get(i));
Word.setText(meaning.get(i));
Arrow.setVisibility(View.INVISIBLE);
}
return view;
Difficulty and Word is two ArrayLists that supplies contents to the TextViews.
In the Activity Daily_Word.java, Dict_Adapter will be applied below:
ListView Daily_Word_Meaning_List=(ListView) findViewById(R.id.Daily_Word_Meaning_List);
String sourceMeaning=c.getString(1);
Log.w("SourceMeaning",sourceMeaning);
ArrayList<String> meaningList=new ArrayList<>(Arrays.asList(sourceMeaning.split("&")));
for (int i=0; i<meaningList.size();i++){
Log.w("Meaning",meaningList.get(i));
}
Dict_Adapter adapter1=new Dict_Adapter(getBaseContext(),meaningList);
Daily_Word_Meaning_List.setAdapter(adapter1);
Then here comes a problem: the app throws NullPointerException:
07-06 23:01:38.513 341-341/com.android.luftschlafer.wordenhancement E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.android.luftschlafer.wordenhancement.Adapters.Dict_Adapter.getView(Dict_Adapter.java:103)
Which means Difficulty.setText(difficulty.get(i)); gives nullpointerexception.
How to overcome this problem? I have already deployed the layout and widgets in "dict_listview_items" already.