0

我在自定义 ListView Adapter android 应用程序中收到 NullPointer 异常。这是我的自定义类,带有一些变量和吸气剂。

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 布局的 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);
4

3 回答 3

2

这是 ListItem 布局的 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

也许错误在这里?

于 2013-09-14T17:13:23.073 回答
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);
于 2013-09-14T17:09:30.713 回答
0

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

因为您正在膨胀list_item_message.xml 而不是list_item_trans.xml并从list_item_trans.xml初始化文本视图

所以改变

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

对此

v = vi.inflate(R.layout.list_item_trans, null);
于 2013-09-14T17:33:35.307 回答