csharp_ant csharp_ant - 4 months ago 16
Java Question

Custom adapter is crashing my app (Android)

I am having some trouble with Android Studio... I am a little rusty but I think that this should work, here's my code.

MainActivity.java

package com.example.testadapter;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

import java.util.List;

public class MainActivity extends AppCompatActivity {

private List<Fruit> fruits;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

fruits.add(new Fruit("Apple"));
fruits.add(new Fruit("Banana"));
fruits.add(new Fruit("Pear"));

FruitListAdapter adapter = new FruitListAdapter(
this, R.layout.list_row, fruits);
ListView lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(adapter);
}
}


Fruit.java

package com.example.testadapter;

public class Fruit {

private String name;

public Fruit(String name) {
this.name = name;
}

public String getName() {
return name;
}
}


FruitListAdapter.java

package com.example.testadapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

public class FruitListAdapter extends ArrayAdapter<Fruit>{

private List<Fruit> fruits;

public FruitListAdapter(Context context, int resource, List<Fruit> objects) {
super(context, resource, objects);
fruits = objects;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).
inflate(R.layout.list_row, parent, false);
}

Fruit fruit = fruits.get(position);

TextView nameText = (TextView) convertView.findViewById(R.id.nameText);
nameText.setText(fruit.getName());

return convertView;
}
}


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.testadapter.MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>


list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="10dp">

<TextView
android:id="@+id/nameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:textSize="24sp" />
</LinearLayout>


The error is:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testeadapter/com.example.testeadapter.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference

Thank you!

Ads Ads
Answer

You have declared the fruits object but you haven't initialized it yet.

So initialize your fruits object,

private List<Fruit> fruits = new ArrayList<Fruit>();
Comments