我正在使用ContentObserver观看ListActivity上的内容更新,如下所示:
protected void onCreate(Bundle savedState)
{
super.onCreate(savedState);
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(TrackHeader.CONTENT_URI, sTrackListProjection, null, null, null);
startManagingCursor(cursor);
this.mAdapter = new TrackHeaderDataAdapter(this, R.layout.track_list_item, cursor, sTrackListProjection, null);
setListAdapter(mAdapter);
Handler handler = new Handler();
mTrackHeaderObserver = new ContentObserver(handler) {
@Override
public boolean deliverSelfNotifications() {
return false;
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
ContentResolver cr = getContentResolver();
mAdapter.changeCursor(cr.query(TrackHeader.CONTENT_URI, sTrackListProjection, null, null, null));
}
};
getContentResolver().registerContentObserver (TrackHeader.CONTENT_URI, true, mTrackHeaderObserver);
}
这个内容观察者似乎没问题 - 它在UI线程上被回调但我在下面的ListView上可以预见到以下随机崩溃:
02-21 14:06:00.440: ERROR/AndroidRuntime(739): java.lang.NullPointerException
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at android.widget.AbsListView.obtainView(AbsListView.java:1276)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at android.widget.ListView.makeAndAddView(ListView.java:1668)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at android.widget.ListView.fillDown(ListView.java:637)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at android.widget.ListView.fillSpecific(ListView.java:1224)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at android.widget.ListView.layoutChildren(ListView.java:1499)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at android.widget.AbsListView.onLayout(AbsListView.java:1113)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at android.view.View.layout(View.java:6830)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at android.view.View.layout(View.java:6830)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at android.view.View.layout(View.java:6830)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at android.view.View.layout(View.java:6830)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at android.view.View.layout(View.java:6830)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at android.view.View.layout(View.java:6830)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at android.view.View.layout(View.java:6830)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at android.view.View.layout(View.java:6830)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at android.view.ViewRoot.performTraversals(ViewRoot.java:996)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at android.os.Looper.loop(Looper.java:123)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at android.app.ActivityThread.main(ActivityThread.java:4363)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at java.lang.reflect.Method.invokeNative(Native Method)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at java.lang.reflect.Method.invoke(Method.java:521)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-21 14:06:00.440: ERROR/AndroidRuntime(739): at dalvik.system.NativeStart.main(Native Method)
02-21 14:09:56.159: ERROR/AndroidRuntime(749): ERROR: thread attach failed
02-21 14:09:59.480: ERROR/AndroidRuntime(760): ERROR: thread attach failed
02-21 14:12:19.449: ERROR/AndroidRuntime(778): ERROR: thread attach failed
02-21 14:12:22.779: ERROR/AndroidRuntime(789): ERROR: thread attach failed
02-21 14:12:26.479: ERROR/gralloc(51): [unregister] handle 0x3f13b8 still locked (state=40000001)
之前有人见过这样的事情 - 被困在这一天几天......
添
答案 0 :(得分:229)
我有类似的堆栈跟踪,并发现在某些情况下我从getView()方法返回null。
答案 1 :(得分:11)
如果您有一个列表并扩展BaseAdapter或Adapter以获取自定义列表,请确保getView返回非空值。
如果你有一个标签视图,你的一个片段是一个覆盖Adapter / BaseAdapter并且getView返回null的列表,你就会遇到这个问题。
答案 2 :(得分:4)
如果使用静态类ViewHolder将视图项存储在适配器中,则可能忘记将viewHolder对象设置为convertView的标记。 E.g:
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.adapter_drivers_and_riders, null);
holder = new ViewHolder();
holder.tvDate = (TextView)convertView.findViewById(R.id.tvDate);
holder.tvTime = (TextView)convertView.findViewById(R.id.tvTime);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
在我的情况下,我没有使用Cursor但是有相同的问题,我想通了我忘了将标签设置为viewHolder来convertView所以当为listview创建第一个适配器视图时,它没关系但是当你滚动和回收机制工作时,它崩溃,因为它无法从convertView恢复viewHolder。
以防万一,我想提一下。
答案 3 :(得分:3)
我没有使用changeCursor()
。而且,由于您用于创建Cursor
的查询与用于“更改”光标的查询相同,因此我将直接转储changeCursor()
调用并调用{{1}在你拥有的requery()
上。