Simple ArrayAdapter导致空指针异常

时间:2013-01-17 20:49:34

标签: android-arrayadapter

我认为它发生的原因是:

我猜我没有错误地实现我的自定义ArrayAdapter,或者我正在为ListView错误地查询

错误:

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 个答案:

答案 0 :(得分:1)

我看到的第一个错误是在EventAdapter.java上,你在那里创建一个新的LinearLayout,因为它应该来自inflate调用。 getView应如下所示:

@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;
}

然后,在布局XML上,您没有为eventName和eventDate定义TextView,因此您可能会在以后获得资源未找到的异常。 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" >
        <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>

希望它有所帮助。