0

我认为它正在发生的原因:

我猜我错误地实现了我的自定义 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>

事件适配器.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;
    }

}
4

1 回答 1

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>

希望能帮助到你。

于 2013-01-17T21:09:56.357 回答