简体   繁体   中英

NullPointer Exception in custom ListView Adapter

I am getting a NullPointer Exception in custom ListView Adapter android application. This is my custom class with a few variables and getter.

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

This is my Main 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();
        // }
        // });

    }
}

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

This is as the name suggests, the 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;
    }
}

I am getting the error on in the getView method of the CustomListAdapter

tvItemName.setText(msg.ItemName);

This is the XML file for ListItem Layout, the name is list_item_trans

So The xml layout name is list_item_trans.xml

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

You are inflating list_item_message.xml but your layout name is list_item_trans.xml

Maybee the error is here ?

List item layout is list_item_trans , but you are inflating list_item_message .

Change this line:

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

Into this:

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

NullPointer Exception in custom ListView Adapter is caused

because you are inflating list_item_message.xml instead of list_item_trans.xml and initializing textviews from list_item_trans.xml

so just change

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

To this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM