2014-02-09 62 views
0

目前試圖創建一個列表視圖定製ArrayAdapter的Android定製ArrayAdapter NullPointerException異常

public class FilesArrayAdapter extends ArrayAdapter<String> { 
private String[] _objects; 

public FilesArrayAdapter(Context context, int resource, String[] objects) { 
    super(context, resource, objects); 
    this._objects = objects; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent){ 
    if(convertView == null){ 
     LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     inflater.inflate(R.layout.entry_layout, null); 
    } 

    String string = _objects[position]; 

    if(string != null){ 
     TextView fNameView = (TextView) convertView.findViewById(R.id.mTextView); 

     if(fNameView != null){ 
      fNameView.setText(string); 
     } 
    } 

    return convertView; 
} 

}

這是佈局R.layout.entry_layout

<?xml version="1.0" encoding="utf-8"?> 

<TextView 
    android:id="@+id/mTextView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 

我綁定列表上的主要活動但它拋出一個NullPointerException這一行:所提到的佈局中存在

TextView fNameView = (TextView) convertView.findViewById(R.id.mTextView); 

文本視圖,並有與佈局文件沒有問題。

感謝

回答

0

getView()方法,你必須做到:

convertView = inflater.inflate(R.layout.entry_layout, null); 
+0

我是個白癡,謝謝。 – Wahtever

+0

這些錯誤只是發生,沒有問題:) –

0

您應該添加:

convertView = inflater.inflate(R.layout.entry_layout, null); 

如下:

public View getView(int position, View convertView, ViewGroup parent){ 
if(convertView == null){ 
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    inflater.inflate(R.layout.entry_layout, null); 
    convertView = inflater.inflate(R.layout.entry_layout, null); 
} 

String string = _objects[position]; 

if(string != null){ 
    TextView fNameView = (TextView) convertView.findViewById(R.id.mTextView); 

    if(fNameView != null){ 
     fNameView.setText(string); 
    } 
} 

return convertView; 

}

相關問題