在我正在測驗應用程序。在我的項目測試結束後,我需要保持評論按鈕。 單擊查看按鈕時,所有出現用於測試的問題必須以正確答案再次顯示一種顏色,錯誤回答一種顏色並超時回答其他顏色。所以我把所有的問題,答案和選定的答案保存在一個數組列表中。 現在處於評論頁面我需要在列表視圖中顯示問題,選項和解釋。因此,我創建了一個listview。但它顯示空指針異常。如何創建一個listview在android中顯示數據?
mycode的:
公共類testreview延伸活動{
private ListView listViewScore = null;
private ListViewAdapter listViewAdapter = null;
public static ArrayList<ArrayList<String>> ourstringList1 = Select.stringList1;
private ArrayList<ArrayList<String>> usernameArrLst = ourstringList1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
listViewScore=(ListView)findViewById(R.id.list);
usernameArrLst = new ArrayList<ArrayList<String>>();
listViewAdapter = new ListViewAdapter();
listViewScore.setAdapter(listViewAdapter);
}
class ListViewAdapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
if(usernameArrLst==null){
return 0;
}
return usernameArrLst.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return usernameArrLst.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
View rowView=view;
if(rowView==null){
LayoutInflater layoutinflate =LayoutInflater.from(testreview.this);
rowView=layoutinflate.inflate(R.layout.listrow, parent, false);
}
TextView textViewName=(TextView)rowView.findViewById(R.id.tv_case);
textViewName.setText((CharSequence) usernameArrLst.get(position));
return rowView;
}
}
}
你從哪裏得到NullPointerException? – Ian 2012-01-04 09:11:39
我在listViewScore.setAdapter(listViewAdapter)處收到空指針異常; – RaagaSudha 2012-01-04 09:12:50
請發佈例外。另外,請檢查** listViewScore **在null之前是否爲listViewScore.setAdapter(listViewAdapter); **。 – Ian 2012-01-04 09:31:44