自定义ListView适配器中的NullPointer异常

时间:2013-09-14 17:01:09

标签: java android listview android-listview custom-controls

我在自定义ListView Adapter android应用程序中得到一个NullPointer异常。 这是我的自定义类,包含一些变量和getter。

public class ViewClass {
    String ItemName, Category, Amount, TotalAmount;
    //get,set methods here
}

这是我的主要活动课程。

public class ViewList extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ArrayList<ViewClass> details = new ArrayList<ViewClass>();

        ViewClass[] v = new ViewClass[5];
        for (int i = 0; i < 5; i++) {
            v[i] = new ViewClass();
            v[i].setItemName("Item Name");
            v[i].setAmount("Rs 1111");

            v[i].setCategory("cateGory");
            v[i].setTotalAmount("aaaa");
            details.add(v[i]);
        }
        ListView msgList = (ListView) findViewById(R.id.MessageList);
        msgList.setAdapter(new CustomListAdapter(details, this));
        // msgList.setOnItemClickListener(new OnItemClickListener() {
        // public void onItemClick(AdapterView<?> a, View v, int position,
        // long id) {
        //
        // String s = (String) ((TextView) v.findViewById(R.id.From))
        // .getText();
        // Toast.makeText(ViewList.this, s, Toast.LENGTH_SHORT).show();
        // }
        // });

    }
}

这是ListItem Layout的XML文件,名称为list_item_trans

   <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/tvBGColor1"
        android:layout_width="10dp"
        android:layout_height="fill_parent"
        android:background="@android:color/background_dark" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/space" />

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

        android:textSize="20sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tvAmount"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"

        android:textSize="20sp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/space" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/tvBGColor2"
        android:layout_width="10dp"
        android:layout_height="fill_parent"
        android:background="@android:color/background_dark" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/space" />

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

        android:textSize="12sp" />

    <TextView
        android:id="@+id/tvTotalAmount"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="right"

        android:textSize="12sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/space" />
</LinearLayout>

顾名思义,这就是CustomListAdapter

public class CustomListAdapter extends BaseAdapter{
    private ArrayList<ViewClass> _data;
    Context _c;

    CustomListAdapter(ArrayList<ViewClass> data, Context c) {
        _data = data;
        _c = c;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return _data.size();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return _data.get(position);
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) _c
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.list_item_message, null);
        }

        TextView tvItemName = (TextView) v.findViewById(R.id.tvItem);
        TextView tvCategory = (TextView) v.findViewById(R.id.tvCat);
        TextView tvAmount = (TextView) v.findViewById(R.id.tvAmount);
        TextView tvTotalAmount = (TextView) v.findViewById(R.id.tvTotalAmount);

        ViewClass msg = _data.get(position);
        tvItemName.setText(msg.ItemName);
        tvCategory.setText("Subject: " + msg.Category);
        tvAmount.setText(msg.Amount);
        tvTotalAmount.setText(msg.TotalAmount);

        return v;
    }
}

我在CustomListAdapter的getView方法中遇到错误

tvItemName.setText(msg.ItemName);

3 个答案:

答案 0 :(得分:2)

  

这是ListItem Layout的XML文件,名称为list_item_trans

所以xml布局名称是list_item_trans.xml

v = vi.inflate(R.layout.list_item_message, null);

您正在膨胀list_item_message.xml,但您的布局名称是list_item_trans.xml

也许错误在这里?

答案 1 :(得分:1)

列表项目布局为list_item_trans,但您正在充气list_item_message

更改此行:

v = vi.inflate(R.layout.list_item_message, null);

进入这个:

v = vi.inflate(R.layout.list_item_trans, null);

答案 2 :(得分:0)

导致自定义ListView适配器中出现NullPointer异常

因为您正在膨胀 list_item_message.xml 而不是 list_item_trans.xml 并从 list_item_trans.xml 初始化 textviews

所以只需改变

 v = vi.inflate(R.layout.list_item_message, null);

到此

v = vi.inflate(R.layout.list_item_trans, null);