每当我的 setadapter 函数被调用时,我的应用程序就会不断崩溃,我不知道为什么,有人可以帮忙吗?我正在运行的模拟器只是说应用程序已意外停止。
这是我的主要
public class MainActivity extends Activity {
private ListView listView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Ride ride_data[] = new Ride[]
{
new Ride("LA Trip", "la man"),
new Ride("SD Trip", "sd man"),
new Ride("SF Trip", "sf man")
};
RideAdapter adapter = new RideAdapter(this,
R.layout.listview_item_row,ride_data);
listView1 = (ListView)findViewById(R.id.listView1);
View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
}
}
这是我的适配器
public class RideAdapter extends ArrayAdapter<Ride>{
Context context;
int layoutResourceId;
Ride data[] = null;
public RideAdapter(Context context, int layoutResourceId, Ride[] data)
{
super(context,layoutResourceId,data);
this.context = context;
this.layoutResourceId = layoutResourceId;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
RideHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new RideHolder();
holder.rideName = (TextView)row.findViewById(R.id.txtRideName);
holder.personName = (TextView)row.findViewById(R.id.txtPersonName);
row.setTag(holder);
}
else
{
holder = (RideHolder)row.getTag();
}
Ride ride = data[position];
holder.rideName.setText(ride.rideName);
holder.personName.setText(ride.personName);
return row;
}
public static class RideHolder
{
TextView rideName;
TextView personName;
}
}
这是我的骑行课
public class Ride {
public String rideName;
public String personName;
public Ride()
{
}
public Ride(String rideName, String personName)
{
super();
this.rideName=rideName;
this.personName=personName;
}
}
最后这是我的列表项视图
<?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"
android:orientation="vertical"
android:padding="10dp"
>
<Textview
android:id="@+id/txtRideName"
android:layout_width ="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
/>
<Textview
android:id="@+id/txtPersonName"
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
/>
</LinearLayout>
任何帮助将不胜感激!