用于ListView的BaseAdapet崩溃

时间:2013-11-17 21:57:33

标签: android baseadapter

我看到了一个如何创建自定义ListView的教程,我做了相同的步骤,但它一直在崩溃,我不知道为什么会这样,我在strings.xml中创建了两个字符串数组,其中包含我命名的标题和描述标题数组“lawstitles”和描述“law”

我的代码:

public class Examples extends Activity {

ListView list;
@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     setContentView(R.layout.examplesandlaws);

        list = (ListView) findViewById(R.id.listView);
        list.setAdapter(new LawsAdapter(this));
    }}
class SingleRow
{
String title;
String description;
int image;

SingleRow(String title, String description, int image)
{
    this.title = title;
    this.description = description;
    this.image = image;
}
}
class LawsAdapter extends BaseAdapter {
ArrayList<SingleRow> list;
Context context;
LawsAdapter(Context c)
{
    context = c;
    list = new ArrayList<SingleRow>();
    Resources res = c.getResources();
    String titles[] = res.getStringArray(R.array.lwastitles);
    String[] descriptions = res.getStringArray(R.array.laws);
    int[] images = {R.drawable.m1,R.drawable.m2,R.drawable.m3};

    for(int i=0;i<10;i++)
    {
        list.add(new SingleRow(titles[i],descriptions[i],images[i]));
    }
}
@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int i) {
    return list.get(i);
}

@Override
public long getItemId(int i) {
    return i;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

    LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row = inflater.inflate(R.layout.singlerow,viewGroup,false);
    TextView title = (TextView) row.findViewById(R.id.textView211);
    TextView description = (TextView) row.findViewById(R.id.textView112);
    ImageView image= (ImageView) row.findViewById(R.id.imageView);
    SingleRow temp = list.get(i);
    title.setText(temp.title);
    description.setText(temp.description);
    image.setImageResource(temp.image);
    return row;
}

}

----------- -----------错误

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.khaled.qiyasbb/com.khaled.qiyasbb.Examples}: android.view.InflateException: Binary XML file line #17: Error inflating class item

-------------- SingleRow.xml ---------------------

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">


    <ImageView
        android:layout_width="wrap_content"
        android:padding="10dp"
        android:src="@drawable/m2"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Title"
        android:id="@+id/textView211"
        android:layout_marginTop="20sp"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/imageView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Subtitle"
        android:textColor="#ffbababa"
        android:id="@+id/textView112"
        android:layout_below="@+id/textView211"
        android:layout_alignRight="@+id/textView211" />
</RelativeLayout>

------------的strings.xml -------------------

<?xml version="1.0" encoding="utf-8"?>

<resources>


    <string-array name="lwastitles">
        <item>Title1</item>
        <item>Title2</item>
        <item>Title3</item>
        </string-array>
    <string-array name="laws">
        <item>SubTitle1</item>
        <item>SubTitle2</item>
        <item>SubTitle3</item>
    </string-array>

</resources>

------------------- examplesandlaws.xml ------------------

<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:orientation="vertical"
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=".MainActivity" >


<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listView" >
</ListView>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

相对布局的xml文件应如下所示。您应该只为每个变量声明一次@+id

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">


    <ImageView
        android:layout_width="wrap_content"
        android:padding="10dp"
        android:src="@drawable/m2"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Title"
        android:id="@+id/textView211"
        android:layout_marginTop="20sp"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@id/imageView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Subtitle"
        android:textColor="#ffbababa"
        android:id="@+id/textView112"
        android:layout_below="@id/textView211"
        android:layout_alignRight="@id/textView211" />
</RelativeLayout>

至于你的第二个错误,你只有3个字符串,所以你的循环应该是

for(int i=0;i<3;i++)
{
    list.add(new SingleRow(titles[i],descriptions[i],images[i]));
}