问题描述
- ListAdapter 的空指针异常
- 我想在List View中显示数据,但是在运行时获得Null Pointer Exception错误。
fList.setAdapter(la);
下面是我用的代码。list 在 Log 中全部打印出来了。
public class Forfattere extends Activity {/** Called when the activity is first created. */ArrayList<String> aList = new ArrayList<String>();@Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.forfattere); ListView fList = (ListView)findViewById(R.id.Author_list); ArrayList<String> a = DBlistbookfestival.getAuthorName(); for(int i=0;i<a.size();i++){ aList.add(a.get(i)); Log.v(""all_data""a.get(i)); } ListAdapter1 la = new ListAdapter1(this R.id.Author_list aList); fList.setAdapter(la); }private class ListAdapter1 extends ArrayAdapter<String> { // --CloneChangeRequired private ArrayList<String> mList; // --CloneChangeRequired public ListAdapter1(Context context int textViewResourceIdArrayList<String> list) { // --CloneChangeRequired super(context textViewResourceId list); this.mList = list; } public View getView(int position View convertView ViewGroup parent){ View view = convertView; try{ if (view == null) { LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = vi.inflate(R.layout.custom_radio null); // --CloneChangeRequired(list_item) } final String listItem = mList.get(position); // --CloneChangeRequired if (listItem != null) { ( (TextView) view.findViewById(R.id.textView111) ).setText(listItem); }}catch(Exception e){ } return view; }}
如何出现的这个异常啊?
解决方案
使用这个方法:
public class Forfattere extends Activity {/** Called when the activity is first created. */ArrayList<String> aList = new ArrayList<String>();ListAdapter1 la = null;ListView fList = null;@Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.forfattere); fList = (ListView)findViewById(R.id.Author_list); ArrayList<String> a = DBlistbookfestival.getAuthorName(); for(int i=0;i<a.size();i++){ aList.add(a.get(i)); Log.v(""all_data""a.get(i)); } la = new ListAdapter1(thisandroid.R.layout.simple_list_item_1 aList); fList.setAdapter(la); }private class ListAdapter1 extends ArrayAdapter<String> { // --CloneChangeRequired private ArrayList<String> mList; // --CloneChangeRequired public ListAdapter1(Context context int textViewResourceIdArrayList<String> list) { // --CloneChangeRequired super(context textViewResourceId list); this.mList = list; } public View getView(int position View convertView ViewGroup parent){ View view = convertView; try{ if (view == null) { LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = vi.inflate(R.layout.custom_radio null); // --CloneChangeRequired(list_item) } final String listItem = mList.get(position); // --CloneChangeRequired if (listItem != null) { ( (TextView) view.findViewById(R.id.textView111) ).setText(listItem); }}catch(Exception e){ } return view; }} }
如果没有申明 list item,请使用android 中默认的。
la = new ListAdapter1(thisandroid.R.layout.simple_list_item_1 aList);
解决方案二:
你的问题出自这一行:
ListView fList = (ListView)findViewById(R.id.Author_list);
findviewbyid 返回 null,意思就是ID是 R.id.Author_list 的布局 R.layout.forfattere 里是没有元素的,查看布局来看listview 是否有这个ID.
时间: 2024-10-27 10:16:04