0

Reason why I think it is happening:

I am guessing I implemented my custom ArrayAdapter incorrectly or I am querying incorrectly for the ListView

Error:

01-17 15:49:56.775: E/AndroidRuntime(531): FATAL EXCEPTION: main
01-17 15:49:56.775: E/AndroidRuntime(531): java.lang.NullPointerException
01-17 15:49:56.775: E/AndroidRuntime(531):  at com.gannett.democratandchronicle.billstrainingcamp.EventAdapter.getView(EventAdapter.java:45)

ScheduleActivity.java

package com.gannett.democratandchronicle.billstrainingcamp;
import java.util.ArrayList;
import java.util.Date;

import android.os.Bundle;
import android.widget.ListView;

public class ScheduleActivity extends BillsCampActivity 
{
    private EventAdapter ea;

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

        Event e1 = new Event("Test 1", new Date(), "Desc");
        Event e2 = new Event("Test 2", new Date(), "Desc");
        Event e3 = new Event("Test 3", new Date(), "Desc");
        Event e4 = new Event("Test 4", new Date(), "Desc");

        ArrayList<Event> schedule = new ArrayList<Event>();
        schedule.add(e1);
        schedule.add(e2);
        schedule.add(e3);
        schedule.add(e4);
        ea = new EventAdapter(this, R.layout.event_item, schedule);
        ListView listView = (ListView)this.findViewById(R.id.schedule_list_view);
        listView.setAdapter(ea);
    }

}

activity_schedule.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" >
    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:id="@+id/schedule_list_view"
        >
    </ListView>
</LinearLayout>

EventAdapter.java

package com.gannett.democratandchronicle.billstrainingcamp;

import java.util.List;

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

public class EventAdapter extends ArrayAdapter<Event> 
{
    private int resource;

    public EventAdapter(Context context, int textViewResourceId,
            List<Event> objects) 
    {
        super(context, textViewResourceId, objects);
        this.resource = textViewResourceId;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        View eventItemView;
        if (convertView == null)
        {
            eventItemView = new LinearLayout(getContext());
            LayoutInflater li = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            li.inflate(resource, null);
        }
        else
        {
            eventItemView = (LinearLayout)convertView;
        }

        TextView eventName = (TextView)eventItemView.findViewById(R.id.eventName);
        TextView eventDate = (TextView)eventItemView.findViewById(R.id.eventDate);

        Event e = this.getItem(position);

        eventName.setText(e.toString());
        eventDate.setText(e.getDate().toString());
        return eventItemView;
    }

}

1 Answer 1

1

The first mistake I see is on EventAdapter.java, where you create a new LinearLayout because it should come from the inflate call. The getView should look like this:

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    View eventItemView = convertView;
    if (eventItemView == null)
    {
        LayoutInflater li = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        eventItemView = li.inflate(resource, false);
    }

    TextView eventName = (TextView)eventItemView.findViewById(R.id.eventName);
    TextView eventDate = (TextView)eventItemView.findViewById(R.id.eventDate);

    Event e = this.getItem(position);

    eventName.setText(e.toString());
    eventDate.setText(e.getDate().toString());
    return eventItemView;
}

Then, on the layout XML you don't define the TextView for eventName and eventDate, so you might get a resource not found exception later. The activity_schedule.xml should be like:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" >
    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:id="@+id/schedule_list_view" >
        <TextView
        android:id="@+id/eventName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
        <TextView
        android:id="@+id/eventDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </ListView>
</LinearLayout>

Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.