BaseAdapet for ListView crashes
-
21-12-2019 - |
Domanda
I saw a tutorial how to create a custom ListView and I did the same exact steps but it keeps crashing and I've not clue why is that, I created two string arrays in strings.xml witch contains titles and descriptions I named the titles array "lawstitles" and for the descriptions "laws"
My code:
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;
}
}
-----------Error-----------
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>
Soluzione
Your xml file for the relative layout should look like that. You should only declare @+id
once for each variable.
<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>
As for your second error, you have only 3 strings so your loop should be
for(int i=0;i<3;i++)
{
list.add(new SingleRow(titles[i],descriptions[i],images[i]));
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow