Java NPE:CursorTreeAdapter$MyCursorHelper.changeCursor(光标,布尔值)和#x27;关于空对象引用
我试图用不同的游标从数据库中获取数据,并将其显示在由组分隔的ExpandableListView中。例如:Java NPE:CursorTreeAdapter$MyCursorHelper.changeCursor(光标,布尔值)和#x27;关于空对象引用,java,android,android-cursorloader,simplecursortreeadapter,Java,Android,Android Cursorloader,Simplecursortreeadapter,我试图用不同的游标从数据库中获取数据,并将其显示在由组分隔的ExpandableListView中。例如: 历史任务(到光标0) 历史1 历史2 地理任务(到光标1) 地理1 地理2 外语任务(至光标2) 外语1 外语2 我正试图使用CursorTreeAdapter来实现这一点,因为我的所有信息都在一个数据库中,它可以管理不同的游标,并在可扩展的ListView中正确显示信息 我遇到的问题是,我在代码的某个地方得到了一个NPE,但我无法检查它在哪里调试,因为它直接更改为另一个文
- 历史任务(到光标0)
- 历史1
- 历史2
- 地理任务(到光标1)
- 地理1
- 地理2
- 外语任务(至光标2)
- 外语1
- 外语2
childrenCursorHelper.changeCursor(childrenCursor, false);
它失败了,所以我不知道这个方法里面发生了什么
我的代码如下:
MtMainActivity.java
PS:Cursor工作正常,我使用唯一的ListView进行了检查,没有问题。我的项目中也有同样的问题。看来,这是一个问题: 看看Android源代码中的方法:
public void setChildrenCursor(int groupPosition, Cursor childrenCursor) {
/*
* Don't request a cursor from the subclass, instead we will be setting
* the cursor ourselves.
*/
MyCursorHelper childrenCursorHelper = getChildrenCursorHelper(groupPosition, false);
/*
* Don't release any cursor since we know exactly what data is changing
* (this cursor, which is still valid).
*/
childrenCursorHelper.changeCursor(childrenCursor, false);
}
在某些情况下,getChildrenCursorHelper将null返回到childrenCursorHelper。我通常在配置更改(例如屏幕旋转)后得到它
为了解决这个问题,我将CursorFilter+CursorTreeAdapter源添加到我的项目中,然后将空检查添加到childrenCursorHelper
public void setChildrenCursor(int groupPosition, Cursor childrenCursor) {
/*
* Don't request a cursor from the subclass, instead we will be setting
* the cursor ourselves.
*/
MyCursorHelper childrenCursorHelper = getChildrenCursorHelper(groupPosition, false);
/*
* Don't release any cursor since we know exactly what data is changing
* (this cursor, which is still valid).
*/
if (childrenCursorHelper != null)
childrenCursorHelper.changeCursor(childrenCursor, false);
}
最后,将我的适配器从fixedCursorTreeAdapter扩展到fixed有关详细信息,请参阅。您不需要向项目中添加CursorTreeAdapter源 只需覆盖setChildrenCursor:
public class MyAdapter extends CursorTreeAdapter {
@Override
public void setChildrenCursor(int groupPosition, Cursor childrenCursor) {
try {
Method getChildrenCursorHelper = CursorTreeAdapter.class.getDeclaredMethod("getChildrenCursorHelper", int.class, boolean.class);
getChildrenCursorHelper.setAccessible(true);
Object childrenCursorHelper = getChildrenCursorHelper.invoke(this, groupPosition, false);
if (childrenCursorHelper != null) {
Method changeCursor = childrenCursorHelper.getClass().getDeclaredMethod("changeCursor", Cursor.class, boolean.class);
changeCursor.setAccessible(true);
changeCursor.invoke(childrenCursorHelper, childrenCursor, false);
}
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
} catch (NoSuchMethodException e) {
}
}
}
at android.widget.CursorTreeAdapter.setChildrenCursor(CursorTreeAdapter.java:164)
public void setChildrenCursor(int groupPosition, Cursor childrenCursor) {
/*
* Don't request a cursor from the subclass, instead we will be setting
* the cursor ourselves.
*/
MyCursorHelper childrenCursorHelper = getChildrenCursorHelper(groupPosition, false);
/*
* Don't release any cursor since we know exactly what data is changing
* (this cursor, which is still valid).
*/
childrenCursorHelper.changeCursor(childrenCursor, false);
}
public void setChildrenCursor(int groupPosition, Cursor childrenCursor) {
/*
* Don't request a cursor from the subclass, instead we will be setting
* the cursor ourselves.
*/
MyCursorHelper childrenCursorHelper = getChildrenCursorHelper(groupPosition, false);
/*
* Don't release any cursor since we know exactly what data is changing
* (this cursor, which is still valid).
*/
if (childrenCursorHelper != null)
childrenCursorHelper.changeCursor(childrenCursor, false);
}
public class MyAdapter extends CursorTreeAdapter {
@Override
public void setChildrenCursor(int groupPosition, Cursor childrenCursor) {
try {
Method getChildrenCursorHelper = CursorTreeAdapter.class.getDeclaredMethod("getChildrenCursorHelper", int.class, boolean.class);
getChildrenCursorHelper.setAccessible(true);
Object childrenCursorHelper = getChildrenCursorHelper.invoke(this, groupPosition, false);
if (childrenCursorHelper != null) {
Method changeCursor = childrenCursorHelper.getClass().getDeclaredMethod("changeCursor", Cursor.class, boolean.class);
changeCursor.setAccessible(true);
changeCursor.invoke(childrenCursorHelper, childrenCursor, false);
}
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
} catch (NoSuchMethodException e) {
}
}
}