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

标签 java android listview android-listview custom-controls

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

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

这是我的主要 Activity 类(class)。

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);

最佳答案

This is the XML file for ListItem Layout, the name is 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

也许错误就在这里?

关于java - 自定义 ListView 适配器中的 NullPointer 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18804178/

相关文章:

java - 如何在 JTextField 中设置财务代码文档过滤器

java - 如何在一个请求中发送文件和重定向?

android - 为什么具有 30 个元素的 ViewPager 和 GridLayout 非常慢?

java - Android:如何使用下载管理器类?

android - 正确使用 twoLineListItem Android

css - jquery Mobile动态缩略图列表-如何创建边框?

java - 消息传递应用程序的 ListView 在滚动后显示错误的 listItem 布局

java - 更新 JTable 日期

java - MySQL语法错误异常 : Access denied for user '' @'localhost' to database 'demo'

Lollipop 之前的 android StateListAnimator