我的ListView
从ListFragment
延伸,并且从BaseAdapter
扩展的customAdapter的子类后,无法将两个数组解析为布局元素。
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/txt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"/>
</LinearLayout>
setListAdapter
功能:
public class ResivedSMS extends ListFragment {
.
.
.
public ResivedSMS() {
testArray1 = new String[] {
"1111111111",
"2222222222",
"3333333333",
"4444444444",
"5555555555",
"6666666666",
};
testArray2 = new String[] {
"AAAAA",
"BBBBB",
"CCCCC",
"DDDDD",
"FFFFF",
"GGGGG",
};
}
.
.
.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ViewResivedSMSDetailes customListView = new ViewResivedSMSDetailes(getActivity(),testArray1,testArray2);
setListAdapter( customListView ); //call the method if listFragment
}
.
.
.
class ViewResivedSMSDetailes extends BaseAdapter
{
private LayoutInflater inflater;
private String[] values1;
private String[] values2;
private class ViewHolder {
TextView txt1;
TextView txt2;
}
public ViewResivedSMSDetailes(Context context,String[] values1,String[] values2)
{
this.values1=values1;
this.values2=values2;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return values1.length;
}
@Override
public Object getItem(int index) {
return values1[index];
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
if(convertView ==null){
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_fragment, null);
holder.txt1 = (TextView)convertView.findViewById(R.id.txt1);
holder.txt2 = (TextView)convertView.findViewById(R.id.txt2);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
holder.txt1.setText(values1[position]);
holder.txt2.setText(values2[position]);
return convertView;
}
}
更新帖子:
list_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/txt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"/>
</LinearLayout>
logCat结果:
08-24 14:48:03.245 851-851/ir.tsms E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at ir.tsms.ResivedSMS$ViewResivedSMSDetailes.getView(ResivedSMS.java:105)
ResivedSMS.java:105是:
holder.txt2.setText(values2[position]);
答案 0 :(得分:0)
从getView()
回调方法中的视图膨胀时看,您没有设置ViewGroup
父级。
尝试更改以下行:
convertView = inflater.inflate(R.layout.list_fragment, null);
要:
convertView = inflater.inflate(R.layout.list_fragment, parent,false);
我建议创建两个视图的共享类,并且只有一个数据集而不是两个数据集,以避免索引超出范围的异常。