Using SeparatedListAdapter for ListView crashes on Android 2.1 but works with 2.2
I am using SeparatedListAdapter as written by Jeff Sharkey . I originally started developing this application on Nexus One, where I have gotten it to work exactly as intended. Now I am moving on testing certain parts, and my application is crashing on all these other phones that are running Android 2.1 . My project Build Target is 2.1-update1 same as all these phones.
Here is the LogCat for the error
01-21 18:24:01.486: ERROR/AndroidRuntime(4615): Uncaught handler: thread main exiting due to uncaught exception
01-21 18:24:01.588: ERROR/AndroidRuntime(4615): java.lang.NullPointerException
01-21 18:24:01.588: ERROR/AndroidRuntime(4615): at android.widget.SimpleAdapter.bindView(SimpleAdapter.java:171)
01-21 18:24:01.588: ERROR/AndroidRuntime(4615): at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:138)
01-21 18:24:01.588: ERROR/AndroidRuntime(4615): at android.widget.SimpleAdapter.getView(SimpleAdapter.java:116)
01-21 18:24:01.588: ERROR/AndroidRuntime(4615): at com.hitpost.GamesList$SeparatedListAdapter.getView(GamesList.java:347)
01-21 18:24:01.588: ERROR/AndroidRuntime(4615): at android.widget.AbsListView.obtainView(AbsListView.java:1274)
01-21 18:24:01.588: ERROR/AndroidRuntime(4615): at android.widget.ListView.makeAndAddView(ListView.java:1668)
01-21 18:24:01.588: ERROR/AndroidRuntime(4615): at android.widget.ListView.fillDown(ListView.java:637)
01-21 18:24:01.588: ERROR/AndroidRuntime(4615): at android.widget.ListView.fillSpecific(ListView.java:1224)
01-21 18:24:01.588: ERROR/AndroidRuntime(4615): at android.widget.ListView.layoutChildren(ListView.java:1511)
01-21 18:24:01.588: ERROR/AndroidRuntime(4615): at android.widget.AbsListView.onLayout(AbsListView.java:1113)
01-21 18:24:01.588: ERROR/AndroidRuntime(4615): at android.view.View.layout(View.java:6830)
01-21 18:24:01.588: ERROR/AndroidRuntime(4615): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:900)
01-21 18:24:01.588: ERROR/AndroidRuntime(4615): at android.view.View.layout(View.java:6830)
01-21 18:24:01.588: ERROR/AndroidRuntime(4615): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
01-21 18:24:01.588: ERROR/AndroidRuntime(4615): at android.view.View.layout(View.java:6830)
01-21 18:24:01.588: ERROR/AndroidRuntime(4615): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
01-21 18:24:01.588: ERROR/AndroidRuntime(4615): at android.view.View.layout(View.java:6830)
01-21 18:24:01.588: ERROR/AndroidRuntime(4615): at android.view.ViewRoot.performTraversals(ViewRoot.java:996)
01-21 18:24:01.588: ERROR/AndroidRuntime(4615): at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
01-21 18:24:01.588: ERROR/AndroidRuntime(4615): at android.os.Handler.dispatchMessage(Handler.java:99)
01-21 18:24:01.588: ERROR/AndroidRuntime(4615): at android.os.Looper.loop(Looper.java:123)
01-21 18:24:01.588: ERROR/AndroidRuntime(4615): at android.app.ActivityThread.main(ActivityThread.java:4363)
01-21 18:24:01.588: ERROR/AndroidRuntime(4615): at java.lang.reflect.Method.invokeNative(Native Method)
01-21 18:24:01.588: ERROR/AndroidRuntime(4615): at java.lang.reflect.Method.invoke(Method.java:521)
01-21 18:24:01.588: ERROR/AndroidRuntime(4615): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
01-21 18:24:01.588: ERROR/AndroidRuntime(4615): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
01-21 18:24:01.588: ERROR/AndroidRuntime(4615): at dalvik.system.NativeStart.main(Native Method)
The relevant part of my code that is referenced by the logcat
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int sectionnum = 0;
for(Object section : this.sections.keySet()) {
Adapter adapter = sections.get(section);
int size = adapter.getCount() + 1;
// check if position inside this section
if(position == 0)
return headers.getView(sectionnum, convertView, parent);
if(position < size) {
//View row = convertView;
ScoreViewHolder viewHolder;
HashMap<String,Object> map = (HashMap<String,Object>) adapter.getItem(position - 1);
if (convertView == null) {
convertView = inflater.inflate(R.layout.league_list_item, null);
viewHolder = new ScoreViewHolder();
viewHolder.row = (RelativeLayout) convertView.findViewById(R.id.ListItem);
viewHolder.checkinImage = (ImageView) viewHolder.row.findViewById(R.id.CheckinLeagueImage);
viewHolder.score1 = (TextView) viewHolder.row.findViewById(R.id.Team1Score);
viewHolder.score2 = (TextView) viewHolder.row.findViewById(R.id.Team2Score);
convertView.setTag(viewHolder);
} else {
viewHolder = (ScoreViewHolder) convertView.getTag();
}
if ((Integer) map.get("position") % 2 == 1)
viewHolder.row.setBackgroundColor(Color.DKGRAY);
else
viewHolder.row.setBackgroundColor(Color.GRAY);
if (map.get("gamestatus").equals("Final")) {
开发者_StackOverflow社区viewHolder.gameFinal();
} else if (map.get("gamestatus").equals("In-Progress")) {
viewHolder.gameInProgress();
} else if (map.get("gamestatus").equals("Pre-Game")) {
viewHolder.gameFinal();
}
viewHolder.row.setOnClickListener(new GameItemListener((String)map.get("id"),
map.get("team2Name") + " " +
map.get("team2Score") + " @ " +
map.get("team1Name") + " " +
map.get("team1Score")
));
return adapter.getView(position - 1, convertView, parent);
}
// otherwise jump into next section
position -= size;
sectionnum++;
}
return null;
}
With the error coming on "return adapter.getView(position - 1, convertView, parent);"
Having spent a fair amount on StackOverflow, I know that its very hard to understand what is going, here is a summary. The ListView displays a list of scores for sports, so each item consists of 2 team names, 2 scores and an icon.
Would appreciate any hints. I have tried looking though the code of SimpleAdapter to figure out what is causing the error but can't figure it out. Thanks.
I have figured this out, and am responding to possibly help fellow Android developers.
The problem is that I was trying to make SeparatedListAdapter getView to do things that the underlying adapter is supposed to be taking care of. I called my adapter a SectionAdapter which extends SimpleAdapter, where I manipulate my view elements as I need to. When I add a section like this
adapter.addSection(currentGameDate, new SectionAdapter(mContext, mActivity, currentGameDateSubset, R.layout.league_list_item, keyNames, keyIds));
Hope that helps.
精彩评论