我正在开发一个很重的 ListView 应用程序,其中的列表项很重。当用户只有几个列表项时,一切正常,但是当用户添加更多时,一些问题开始浮出水面。与列表项的交互变得更加困难,有时当单击一个项目时,用户会获得触摸效果但没有效果。当访问位置时(这是在使用上下文操作栏http://developer.android.com/guide/topics/ui/menus.html#CAB时完成的),单击会引发 NullPointerException。我很确定这是因为我的应用程序中的 GC 杀死了资源。有没有关于如何避免这种行为的一般提示或可能有某种解决方法来解决这个问题?
谢谢!
编辑:
Logcat 崩溃:
01-07 15:59:39.219: E/AndroidRuntime(8003): FATAL EXCEPTION: main
01-07 15:59:39.219: E/AndroidRuntime(8003): java.lang.NullPointerException
01-07 15:59:39.219: E/AndroidRuntime(8003): at com.simon.holocountownapp.MainActivity$1.onItemCheckedStateChanged(MainActivity.java:104)
01-07 15:59:39.219: E/AndroidRuntime(8003): at android.widget.AbsListView$MultiChoiceModeWrapper.onItemCheckedStateChanged(AbsListView.java:6037)
01-07 15:59:39.219: E/AndroidRuntime(8003): at android.widget.AbsListView.setItemChecked(AbsListView.java:1013)
01-07 15:59:39.219: E/AndroidRuntime(8003): at android.widget.AbsListView.performLongPress(AbsListView.java:2807)
01-07 15:59:39.219: E/AndroidRuntime(8003): at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:2765)
01-07 15:59:39.219: E/AndroidRuntime(8003): at android.os.Handler.handleCallback(Handler.java:725)
01-07 15:59:39.219: E/AndroidRuntime(8003): at android.os.Handler.dispatchMessage(Handler.java:92)
01-07 15:59:39.219: E/AndroidRuntime(8003): at android.os.Looper.loop(Looper.java:137)
01-07 15:59:39.219: E/AndroidRuntime(8003): at android.app.ActivityThread.main(ActivityThread.java:5191)
01-07 15:59:39.219: E/AndroidRuntime(8003): at java.lang.reflect.Method.invokeNative(Native Method)
01-07 15:59:39.219: E/AndroidRuntime(8003): at java.lang.reflect.Method.invoke(Method.java:511)
01-07 15:59:39.219: E/AndroidRuntime(8003): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
01-07 15:59:39.219: E/AndroidRuntime(8003): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
01-07 15:59:39.219: E/AndroidRuntime(8003): at dalvik.system.NativeStart.main(Native Method)
第 104 行(在 logcat 中引用)调用 listView.getChildAt(position) 并且这很可能返回导致崩溃的 null。
布局非常繁重(200 多行代码),所以我将其发布到 pastebin。 http://pastebin.se/KTmCA0jc
适配器:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
CountdownViewHolder holder;
CountdownItem ci = mTitle.get(position);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_layout, parent,
false);
holder = new CountdownViewHolder();
holder.mTitle = (TextView) convertView.findViewById(R.id.textPrim);
holder.mSubtitle = (TextView) convertView
.findViewById(R.id.textSec);
holder.mOverflow = (ImageButton) convertView
.findViewById(R.id.overflow);
holder.mInfo = (ImageButton) convertView.findViewById(R.id.info);
holder.expand_layout = (LinearLayout) convertView
.findViewById(R.id.expand_layout);
holder.mExpDay = (TextView) convertView.findViewById(R.id.exp_day);
holder.mExpSec = (TextView) convertView.findViewById(R.id.exp_sec);
holder.mDayProgress = (ProgressBar) convertView
.findViewById(R.id.day_progress);
holder.mMonthProgress = (ProgressBar) convertView
.findViewById(R.id.month_progress);
holder.mYearText = (TextView) convertView
.findViewById(R.id.year_text);
holder.day_help = (TextView) convertView
.findViewById(R.id.day_help);
holder.month_help = (TextView) convertView
.findViewById(R.id.month_help);
holder.setTag(position);
holder.setupInfoClickActions();
holder.setupOnClickListener();
holder.setTitle(ci.getTitle());
holder.setSubtitle(ci.getSubtitle());
holder.fixImageAndText(position);
convertView.setTag(holder);
} else {
holder = (CountdownViewHolder) convertView.getTag();
}
holder.manageExpand(ci.getExpand(), ctx, position, mTitle.size());
return convertView;
}