我正在使用Jeff Sharkey编写的SeparatedListAdapter。我最初开始在Nexus One上开发这个应用程序,在那里我完全按照预期的方式工作。现在我正在测试某些部件,我的应用程序正在崩溃所有其他运行Android 2.1的手机。我的项目Build Target是2.1-update1,与所有这些手机相同。
这是错误的LogCat
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)
我的代码的相关部分,由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")) {
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;
}
出现错误“return adapter.getView(position-1,convertView,parent);”
在StackOverflow上花了相当多的钱,我知道很难理解发生了什么,这里是一个总结。 ListView显示体育赛事的分数列表,因此每个项目包含2个团队名称,2个分数和一个图标。
会感激任何提示。我试过看看SimpleAdapter的代码,找出导致错误的原因,但无法弄明白。感谢。
答案 0 :(得分:1)
我已经想到了这一点,并且正在回应可能会帮助其他Android开发人员。
问题是我试图使SeparatedListAdapter getView执行底层适配器应该处理的事情。我给我的适配器调了一个SectionAdapter,它扩展了SimpleAdapter,我根据需要操作我的视图元素。当我添加这样的部分时
adapter.addSection(currentGameDate, new SectionAdapter(mContext, mActivity, currentGameDateSubset, R.layout.league_list_item, keyNames, keyIds));
希望有所帮助。