在我的应用程序中,我向自定义ArrayAdapter提供了一个Arraylist,它负责对名为' slot_item_row'的布局进行充气。所有这一切都在这里工作正常,但在我访问特定布局的Textviews后,我得到错误和应用程序崩溃,日志描述无法访问textview资源。 但是当我评论文本视图访问部分时,所有它都显示正常。 的 ArrayAdapter:
public class SlotListAdapter extends ArrayAdapter<slots> {
Context context;
List<slots> sl;
public SlotListAdapter(Context context,int textViewResourceId ,List<slots> Slots) {
super(context, textViewResourceId, Slots);
this.context = context;
this.sl = Slots;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = vi.inflate(R.layout.slot_block, null);
slots slot = sl.get(position);
TextView tit = (TextView) view.findViewById(R.id.txt1s);
tit.setText(slot.name);
TextView num = (TextView) view.findViewById(R.id.txt2s);
num.setText(slot.slotNum);
TextView status = (TextView) view.findViewById(R.id.txt3s);
status.setText(slot.status);
return view;
}
}
slot_block.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:weightSum="1">
<ImageView
android:layout_width="100sp"
android:layout_height="100sp"
android:id="@+id/imageView2"
android:background="@drawable/parking"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.45">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Name"
android:id="@+id/textView9" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Slot"
android:id="@+id/textView21" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Status"
android:id="@+id/textView48" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="109dp"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txt1s" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txt2s" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txt3s" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
ListActivity:
public class ShowSlots extends ListActivity {
private List<slots> rts;
String username,parkingName,price,lat,lon,available;
int availableParkings;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_slot);
Bundle extras = getIntent().getExtras();
if (extras != null) {
username = extras.getString("USERNAME");
parkingName = extras.getString("PARKINGNAME");
price = extras.getString("PRICE");
lat = extras.getString("LAT");
lon = extras.getString("LON");
available = extras.getString("AVAILABLE");
availableParkings = Integer.parseInt(available);
}
rts= new ArrayList<slots>();
for(int i=0;i<availableParkings;i++){
rts.add(new slots(parkingName,i+1,"Available"));
}
refreshDisplay();
}
private void refreshDisplay(){
if(rts.size()==0){
Toast.makeText(this, "No Reminders in Database", Toast.LENGTH_LONG).show();
}
ArrayAdapter<slots> remAdpt = new SlotListAdapter(this,R.layout.slot_block,rts);
setListAdapter(remAdpt);
}
}
activity_slot.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.sol.irad.parkme.SlotActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/txtMessage"
android:text="Parking Slots"
android:textColor="#00318c"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txtMessage"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
答案 0 :(得分:1)
slotNum
和status
字段的类型是什么?如果它们是int
值,则无法像这样调用setText
:
TextView num = (TextView) view.findViewById(R.id.txtSlotSA);
num.setText(slot.slotNum);
TextView status = (TextView) view.findViewById(R.id.txtStatusSA);
status.setText(slot.status);
问题是当您使用int
方法使用setText
时,您传递的是资源ID。该ID不存在,ResourceNotFoundException
将被抛出。所以你需要改变你的代码:
TextView num = (TextView) view.findViewById(R.id.txtSlotSA);
num.setText(String.valueOf(slot.slotNum));
TextView status = (TextView) view.findViewById(R.id.txtStatusSA);
status.setText(String.valueOf(slot.status));