Reputation: 33
This question has been resolved. Figured out it was a simple typo and I was referencing the wrong layout file.
I am trying to create a custom adapter for a list view so that I can display a (non-editable) rating bar with a rating value that my program got from a database for each item in the menu. When I run it, it crashes and I can't figure out why.
Here is the method trying to create the listview. SetBogusFoodArray2()
returns an ArrayList of menu items that are defined in another class. That is all working properly.
private void setupMenu2(){
ArrayList<MenuItem> menu = setBogusFoodArray2();
MenuAdapter myMenuAdapter = new MenuAdapter(this, menu);
ListView menuList = (ListView) findViewById(R.id.listView1);
menuList.setAdapter(myMenuAdapter);
}
Here is the definition of my adapter.
public class MenuAdapter extends BaseAdapter{
private Context context;
private List<MenuItem> myMenu = new ArrayList<MenuItem>();
public MenuAdapter(Context context, ArrayList<MenuItem> menu){
context = context;
myMenu = menu;
}
public int getCount() {
return myMenu.size();
}
public Object getItem(int position) {
return myMenu.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View myView = convertView;
MenuItem myFood = (MenuItem) getItem(position);
if (convertView == null){
LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView = inf.inflate(R.layout.hall_list_item, null);
}
TextView name = (TextView) myView.findViewById(R.id.textView1);
TextView hall = (TextView) myView.findViewById(R.id.textView2);
RatingBar rating = (RatingBar) myView.findViewById(R.id.ratingBar1);
name.setText(myFood.myName);
hall.setText(myFood.myHall);
rating.setRating(myFood.myRating);
return myView;
}
}
Any help would be greatly appreciated! Here are the first few errors in the logcat. If you want to see more, there is a lot more red!
11-02 18:10:36.284: E/AndroidRuntime(619): FATAL EXCEPTION: main 11-02 18:10:36.284: E/AndroidRuntime(619): java.lang.NullPointerException 11-02 18:10:36.284: E/AndroidRuntime(619): at edu.calvin.cs.dininghall4.MenuAdapter.getView(MenuAdapter.java:49) 11-02 18:10:36.284: E/AndroidRuntime(619): at android.widget.AbsListView.obtainView(AbsListView.java:2267) 11-02 18:10:36.284: E/AndroidRuntime(619): at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
Here is the relevant xml file:
<?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"
android:background="#ff444444" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffffff" />
<RatingBar
android:id="@+id/ratingBar1"
android:isIndicator="true"
android:rating = "2.5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
style="?android:attr/ratingBarStyleSmall"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/ratingBar1"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#ffffffff" />
</RelativeLayout>
By process of elimination and commenting out code, the lines that are the problem are:
name.setText(myFood.myName);
hall.setText(myFood.myHall);
rating.setRating(myFood.myRating);
It works with these lines commented out, but the text is simply the default. I tried simply inserting values instead of variables, but it still crashes. It seems that any reference to these (setText("Bacon"), setBackgroundColor, setAllCaps, etc) causes the app to crash.
Upvotes: 0
Views: 1776
Reputation: 6368
The code is actually pretty good, logically
measureHeightOfChildren
That guy is nasty. Internally, are you using a LinearLayout? Honestly, I'm not sure if this is the answer, but...
What sometimes happens with LinearLayouts is that the views inside somehow become larger than the layout's allotted space, which sounds weird because ListViews, in theory, are supposed to have unlimited height. I wonder if the height is set to match_parent or something
Also, I would consider checking out the context. Consider putting .getSystemService(Context.LAYOUT_INFLATER_SERVICE); code in the constructor and then don't store the context
Upvotes: 0
Reputation: 13327
it seems that on of these lines throw the NullPointerException
in your getView
method
name.setText(myFood.myName);
hall.setText(myFood.myHall);
rating.setRating(myFood.myRating);
first thing check if the id of views matches the ones in your layout file "hall_list_item.xml" you can help us by posting the xml file.
Upvotes: 1
Reputation: 5636
next time post a logcat. for now, you need to change your myView
to:
myView = inf.inflate(R.layout.hall_list_item, parent, false);
also i suggest you let your constructor do the LayoutInflater
instantiation, because as of now - it's done for every row in your listview.
Upvotes: 1