我认为它正在发生的原因:
我猜我错误地实现了我的自定义 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;
}
}