程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> android-ListAdapter 的空指針異常

android-ListAdapter 的空指針異常

編輯:編程綜合問答
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>();
@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);
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved