我想在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>();
@Override
public 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 textViewResourceId,ArrayList<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;
@Override
public 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(this,android.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 textViewResourceId,ArrayList<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(this,android.R.layout.simple_list_item_1, aList);