为什么mAlphabetIndexer.getSectionForPosition与NPE崩溃?

时间:2014-03-02 20:49:54

标签: android android-cursoradapter

我有一个名为ContactListFragment的课程,让用户从联系簿中选择电话号码。我的适配器是游标适配器,如下所示

private class ContactsAdapter extends CursorAdapter implements SectionIndexer {
…

@Override
        public int getSectionForPosition(int i) {
            if (getCursor() == null) {
                return 0;
            }
            return mAlphabetIndexer.getSectionForPosition(i);
        }
...
}

我在第return mAlphabetIndexer.getPositionForSection(i)行得到一个NullPointerException。我不确定在这种情况下是什么构成了足够的信息,所以我在下面包括堆栈跟踪。有谁知道我可能做错了什么?我的字母表都是大写的,但我使用的是API 11而不是API 14,所以我不能在{/ 1>中使用android:textAllCaps

<style name="addressHeader" parent="@android:style/TextAppearance.Small">
        <item name="android:textAllCaps">true</item>
        <item name="android:textStyle">bold</item>
    </style>

这是字母

<string name="alphabet">ABCDEFGHIJKLMNOPQRSTUVWXYZ</string>

作为参考,我使用Google Tutorial中的ContactListFragment作为指南。我所知道的唯一区别是我提到的API级别。

FATAL EXCEPTION: main
 java.lang.NullPointerException
    at android.widget.AlphabetIndexer.compare(AlphabetIndexer.java:128)
    at android.widget.AlphabetIndexer.getSectionForPosition(AlphabetIndexer.java:265)
    at com.company.app.fragments.ContactsListFragment$ContactsAdapter.getSectionForPosition(ContactsListFragment.java:801)
    at android.widget.FastScroller.getThumbPositionForListPosition(FastScroller.java:648)
    at android.widget.FastScroller.onScroll(FastScroller.java:458)
    at android.widget.AbsListView.invokeOnItemScrollListener(AbsListView.java:1358)
    at android.widget.ListView.layoutChildren(ListView.java:1707)
    at android.widget.AbsListView.onLayout(AbsListView.java:2141)
    at android.view.View.layout(View.java:13849)
    at android.view.ViewGroup.layout(ViewGroup.java:4364)
    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1003)
    at android.view.View.layout(View.java:13849)
    at android.view.ViewGroup.layout(ViewGroup.java:4364)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:469)
    at android.view.View.layout(View.java:13849)
    at android.view.ViewGroup.layout(ViewGroup.java:4364)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:469)
    at android.view.View.layout(View.java:13849)
    at android.view.ViewGroup.layout(ViewGroup.java:4364)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1535)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1448)
    at android.view.View.layout(View.java:13849)
    at android.view.ViewGroup.layout(ViewGroup.java:4364)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:469)
    at android.view.View.layout(View.java:13849)
    at android.view.ViewGroup.layout(ViewGroup.java:4364)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1535)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1448)
    at android.view.View.layout(View.java:13849)
    at android.view.ViewGroup.layout(ViewGroup.java:4364)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:469)
    at android.view.View.layout(View.java:13849)
    at android.view.ViewGroup.layout(ViewGroup.java:4364)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1895)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1715)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1010)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4255)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
    at android.view.Choreographer.doFrame(Choreographer.java:525)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4849)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

我遇到了这个错误,因为我的游标数据null进入了我在AlphabetIndexer的第二个参数中传递的columnIndex。

您可以使用DatabaseUtils.dumpCursorToString(cursor))进行调试。