NPE:在空对象引用上执行CursorTreeAdapter $ MyCursorHelper.changeCursor(Cursor,boolean)'

4
我正在尝试从我的数据库中获取不同游标的数据,并显示在分组的ExpandableListView中。就像下面这样: 历史任务(到游标0) - 历史1 - 历史2
地理任务(到游标1) - 地理1 - 地理2
外语任务(到游标2) - 外语1 - 外语2
我试图使用CursorTreeAdapter实现它,因为我所有的信息都在数据库中,它可以管理不同的游标并在ExpandableListView中正确显示信息。
但问题是,我的代码某些部分会出现NPE,但我无法通过调试找到它的位置,因为它直接切换到另一个文件而无法知道发生了什么。
当我在CursorTreeAdapter :: setChildrenCursor方法中进行调试时,只有当我步进时才会遇到该问题。
childrenCursorHelper.changeCursor(childrenCursor, false);

出现了错误,所以我不知道这个方法内部发生了什么。

我的代码如下:

MtMainActivity.java

public class MtMainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {

    @InjectView(R.id.elvTaskList)
    protected ExpandableListView elvTaskList;
    private TaskCursorAdapter taskListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        taskListAdapter = new TaskCursorAdapter(null, this);
        elvTaskList.setAdapter(taskListAdapter);
        getSupportLoaderManager().initLoader(MtLoaders.HISTORY_TASK, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        switch (id) {
            case MtLoaders.HISTORY_TASK:
                return mtLoaderFactory.createHistoryTaskLoader();
        }
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        switch (loader.getId()) {
            case MtLoaders.HISTORY_TASK:
                taskListAdapter.setChildrenCursor(0, cursor);
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        switch (loader.getId()) {
            case MtLoaders.HISTORY_TASK:
                taskListAdapter.setChildrenCursor(0, null);
        }
    }

TaskCursorAdapter.java

public class TaskCursorAdapter extends CursorTreeAdapter {

    private final Cursor cursor;

    public TaskCursorAdapter(Cursor cursor, Context context) {
        super(cursor, context);
        this.cursor = cursor;
    }

    @Override
    protected Cursor getChildrenCursor(Cursor groupCursor) {
        return cursor;
    }

    @Override
    protected View newGroupView(Context context, Cursor cursor, boolean isExpanded, ViewGroup parent) {
        final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final TextView tvHeader = (TextView) layoutInflater.inflate(R.layout.mt_task_item_subtitle, parent, false);
        tvHeader.setText(">> Insert name here <<");
        return tvHeader;
    }

    @Override
    protected void bindGroupView(View view, Context context, Cursor cursor, boolean isExpanded) { /* nothing */ }

    @Override
    protected View newChildView(Context context, Cursor cursor, boolean isLastChild, ViewGroup parent) {
        final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View view = layoutInflater.inflate(R.layout.mt_task_item, parent, false);

        return view;
    }

    @Override
    protected void bindChildView(View view, Context context, Cursor cursor, boolean isLastChild) { /* nothing */ }
}

Error

E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: project.android, PID: 11592
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CursorTreeAdapter$MyCursorHelper.changeCursor(android.database.Cursor, boolean)' on a null object reference
        at android.widget.CursorTreeAdapter.setChildrenCursor(CursorTreeAdapter.java:164)
        at project.android.ui.MtMainActivity.onLoadFinished(MtMainActivity.java:215)
        at project.android.ui.MtMainActivity.onLoadFinished(MtMainActivity.java:29)
        at android.support.v4.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:427)
        at android.support.v4.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:395)
        at android.support.v4.content.Loader.deliverResult(Loader.java:104)
        at android.support.v4.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:223)
        at android.support.v4.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:61)
        at android.support.v4.content.ModernAsyncTask.finish(ModernAsyncTask.java:461)
        at android.support.v4.content.ModernAsyncTask.access$500(ModernAsyncTask.java:47)
        at android.support.v4.content.ModernAsyncTask$InternalHandler.handleMessage(ModernAsyncTask.java:474)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

PS:光标正常工作,我使用了一个唯一的ListView进行了检查,这方面没有问题。

2个回答

1

我在我的项目中遇到了同样的问题。看起来,这是CursorTreeAdapter的一个问题:

at android.widget.CursorTreeAdapter.setChildrenCursor(CursorTreeAdapter.java:164)

看一下 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中添加了null检查。
    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);
}

最后,将我的适配器从固定的CursorTreeAdapter中扩展。
详情请参见this commit

1

你不需要将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) {
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接