关闭后重新初始化游标会返回nullpointerexception

时间:2015-09-12 02:54:11

标签: android

我有一个光标对象,用于返回用户手机上的联系人。完成后,我通过调用cursor.close();关闭它。我允许用户在必要时刷新内容,或者删除或添加联系人。我称之为以下方法

public void refreshContent(final boolean fromOnCreate) {
    refresh();
    clear();
    adapter = null;
    android.os.Handler handler = new android.os.Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (cr == null) {
                Toast.makeText(returnContext(), "CR NULL_refreshCheckBeginningOfRun", Toast.LENGTH_SHORT).show();
            } else {
                fetchContacts();
                adapter = new LibraryAdapter(returnContext());
                mRecyclerView.setAdapter(adapter);
                if (fromOnCreate) {
                    mSwipeLayout.setRefreshing(false);
                }
            }
        }
    }, 1000);
}

public void refresh(){
    if (cursor.isClosed()){
        cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
    }
}

当用户向下滑动以刷新时,该方法可以正常工作,但如果在我添加联系人或删除联系人时调用它,则游标返回null。

这是LogCat:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.content.ContentResolver.query(android.net.Uri, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String)' on a null object reference
        at com.vinodh.android.kontakt.MainActivity.refresh(MainActivity.java:127)
        at com.vinodh.android.kontakt.MainActivity.refreshContent(MainActivity.java:104)
        at com.vinodh.android.kontakt.LibraryAdapter$MyViewHolder$1$1.onClick(LibraryAdapter.java:167)
        at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:1017)
        at android.widget.AdapterView.performItemClick(AdapterView.java:300)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1247)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3215)
        at android.widget.AbsListView.onTouchUp(AbsListView.java:4021)
        at android.widget.AbsListView.onTouchEvent(AbsListView.java:3793)
        at android.view.View.dispatchTouchEvent(View.java:8444)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2158)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2436)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2178)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2436)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2178)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2436)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2178)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2436)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2178)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2436)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2178)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2386)
        at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1727)
        at android.app.Dialog.dispatchTouchEvent(Dialog.java:765)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2335)
        at android.view.View.dispatchPointerEvent(View.java:8655)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4238)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4094)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3635)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3694)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3660)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3773)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3668)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3830)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3640)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3694)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3660)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3668)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3640)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5940)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5908)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5872)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6030)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:211)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:153)
        at android.os.Looper.loop(Looper.java:131)
        at android.app.ActivityThread.main(ActivityThread.java:5696)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Metho

导致此问题的行是:cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);

为什么我收到此错误?

编辑:这是我初始化cr对象的方式

cr = context.getContentResolver();
    cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
    if (cr != null) {
        fetchContacts();
    }

0 个答案:

没有答案