Urja Pawar Urja Pawar - 7 months ago 36
Java Question

App crashing due to setImageResource(rowItem.getImageId())

I am working on custom list view using arrayadapter,The app crashes with the following log

04-14 02:11:20.231 1584-1584/com.urjapawar.project E/AndroidRuntime: at com.urjapawar.project.CustomAdapter.getView(CustomAdapter.java:46)


and at line 46 I have

holder.imageView.setImageResource(rowItem.getImageId());


Here is the code of my CustomAdapter.java

import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomAdapter extends ArrayAdapter<RowItem> {
Context context;

public CustomAdapter(Context context, int resourceId,
List<RowItem> items) {
super(context, resourceId, items);
this.context = context;
}

/*private view holder class*/
private class ViewHolder {
ImageView imageView;
TextView txtTitle;
TextView txtDesc;
}

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
RowItem rowItem = getItem(position);

LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item,parent,false);
holder = new ViewHolder();
holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();

holder.txtDesc.setText(rowItem.getDesc());
holder.txtTitle.setText(rowItem.getTitle());
holder.imageView.setImageResource(rowItem.getImageId());


return convertView;
}
}


Here is RowItem.java

public class RowItem {
private int imageId;
private String title;
private String desc;

public RowItem(int imageId, String title, String desc) {
this.imageId = imageId;
this.title = title;
this.desc = desc;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return title + "\n" + desc;
}
}


Here is list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/icon"
android:layout_width="80dp"
android:layout_height="80dp"
android:contentDescription="@string/image"
android:paddingLeft="10dp"
android:paddingRight="10dp" />

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/icon"
android:paddingBottom="10sp"
android:textColor="#CC0033"
android:textSize="16sp" />

<TextView
android:id="@+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:layout_toRightOf="@+id/icon"
android:paddingLeft="10sp"
android:paddingRight="10sp"
android:layout_toEndOf="@+id/icon"
android:paddingStart="10sp"
android:paddingEnd="10sp"
android:textColor="#3399FF"
android:textSize="14sp" />
</RelativeLayout>


MainActivity.Java

public class MainActivity extends AppCompatActivity implements OnItemClickListener {
public static final String[] titles = new String[] { "H C Verma",
"P Bahadur", "I E Irodov", "M L Khanna",
"O P Tondon", "Morrison & Boyd", "Halliday Resnick",
"R D Sharma", "S L Loney", "Hall and Knight", "Krotov", "I A Maron" };

public static final String[] descriptions = new String[] {

"Good For clearing concepts of physics",
"Variety of examples on particular concept",
"Read to become expert in physics",
"Read to become an expert in mathematics",
"A good reference for detailed stuff",
"Read to become an expert in chemistry",
"Interesting and fundamental book of physics",
"Mandatory book forJEE aspirants",
"Another good book for high rankers",
"Simple and clear concepts of chemistry",
"favourite one for physics lovers",
"Read this for practice purposes"

};

public static final Integer[] images = { R.drawable.hcv,
R.drawable.bahadur, R.drawable.irodov, R.drawable.mlkhanna,
R.drawable.tondon, R.drawable.boyd, R.drawable.resnick, R.drawable.sharma,
R.drawable.loney, R.drawable.knight, R.drawable.krotov, R.drawable.maron};

ListView listView;
List<RowItem> rowItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
rowItems = new ArrayList<>();
for (int i = 0; i < titles.length; i++) {
RowItem item = new RowItem(images[i], titles[i], descriptions[i]);
rowItems.add(item);
}

listView = (ListView) findViewById(R.id.list);
CustomAdapter adapter = new CustomAdapter(this,
R.layout.list_item, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);


}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast toast = Toast.makeText(getApplicationContext(),
"Item " + (position + 1) + ": " + rowItems.get(position),
Toast.LENGTH_SHORT);
toast.show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}


and one more thing, when I have copied the code and added necessary files in drawable folder from http://theopentutorials.com/tutorials/android/listview/android-custom-listview-with-image-and-text-using-arrayadapter/
The code was working fine! All I have done is changed the name of objects and increased their number.

Answer

The only thing troubling me was the size of the images I was using, we should use small size images otherwise it says something like

bitmap size exceeds VM budget 

Thank you everyone for helping me

Comments