我有一个listView需要定制的适配器-Dict_Adapter。 Dict_Adapter的构造函数之一是:
public Dict_Adapter(Context c,ArrayList<String> Meanings){
context=c;
meaning=Meanings;
type=2;
}
在适配器的getView中,应用程序将设置两个TextView和一个ImageView,如下所示:
@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;
难度和Word是两个向TextViews提供内容的ArrayLists。
在Activity Daily_Word.java中,Dict_Adapter将在下面应用:
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);
然后出现问题:应用程序抛出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)
这意味着什么 Difficulty.setText(difficulty.get(I)); 给出了nullpointerexception。
如何克服这个问题?我已经在“dict_listview_items”中部署了布局和小部件。