Android兼容性库CursorLoader:java.lang.IllegalStateException:游标已关闭

前端之家收集整理的这篇文章主要介绍了Android兼容性库CursorLoader:java.lang.IllegalStateException:游标已关闭前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前正在使用 Android兼容性库中的片段内的CursorLoader.
几乎每次,在Droid 2(我无法在Nexus One或Sensation上重现),似乎游标正在关闭太快,这是导致各种错误.不幸的是,我的代码中没有任何帮助的痕迹(因为问题是为什么Cursor被关闭,而不是Android通知),所以我真的很想弄清楚出了什么问题.有其他人遇到这个问题吗? (或有想法可能发生).

一些堆栈跟踪:

java.lang.IllegalStateException: Cursor is closed                                    
    at android.database.sqlite.sqliteCursor.fillWindow(sqliteCursor.java:278)         
    at android.database.sqlite.sqliteCursor.onMove(sqliteCursor.java:255)             
    at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:187)        
    at android.database.CursorWrapper.moveToPosition(CursorWrapper.java:187)          
    at android.support.v4.widget.CursorAdapter.getItemId(CursorAdapter.java:226)      
    at android.widget.AbsListView$PerformClick.run(AbsListView.java:1721)             
    at android.os.Handler.handleCallback(Handler.java:587)                            
    at android.os.Handler.dispatchMessage(Handler.java:92)                            
    at android.os.Looper.loop(Looper.java:143)                                        
    at android.app.ActivityThread.main(ActivityThread.java:4717)                      
    at java.lang.reflect.Method.invokeNative(Native Method)                           
    at java.lang.reflect.Method.invoke(Method.java:521)                               
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)                   
    at dalvik.system.NativeStart.main(Native Method)           

java.lang.IllegalStateException: Cursor is closed                                                          
    at android.database.sqlite.sqliteCursor.fillWindow(sqliteCursor.java:278)                               
    at android.database.sqlite.sqliteCursor.onMove(sqliteCursor.java:255)                                   
    at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:187)                              
    at android.database.CursorWrapper.moveToPosition(CursorWrapper.java:187)                                
    at android.support.v4.widget.CursorAdapter.getItemId(CursorAdapter.java:226)                            
    at android.widget.AdapterView.getItemIdAtPosition(AdapterView.java:745)                                 
    at android.widget.AdapterView.setSelectedPositionInt(AdapterView.java:1081)                             
    at android.widget.AbsListView.onTouchEvent(AbsListView.java:2207)                                       
    at android.widget.ListView.onTouchEvent(ListView.java:3377)                                             
    at android.view.View.dispatchTouchEvent(View.java:3766)                                                 
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:897)                                        
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)                                        
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)                                        
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)                                        
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)                                        
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)                                        
    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1800)
    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1140)          
    at android.app.Activity.dispatchTouchEvent(Activity.java:2105)                                          
    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1784)     
    at android.view.ViewRoot.handleMessage(ViewRoot.java:1794)                                              
    at android.os.Handler.dispatchMessage(Handler.java:99)                                                  
    at android.os.Looper.loop(Looper.java:143)                                                              
    at android.app.ActivityThread.main(ActivityThread.java:4717)                                            
    at java.lang.reflect.Method.invokeNative(Native Method)                                                 
    at java.lang.reflect.Method.invoke(Method.java:521)                                                     
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)                      
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)                                         
    at dalvik.system.NativeStart.main(Native Method)     

java.lang.RuntimeException: Unable to pause activity : java.lang.IllegalStateException: Cursor is closed
    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3438)                         
    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3395)                         
    at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3378)                          
    at android.app.ActivityThread.access$2700(ActivityThread.java:129)                                   
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2124)                              
    at android.os.Handler.dispatchMessage(Handler.java:99)                                               
    at android.os.Looper.loop(Looper.java:143)                                                           
    at android.app.ActivityThread.main(ActivityThread.java:4717)                                         
    at java.lang.reflect.Method.invokeNative(Native Method)                                              
    at java.lang.reflect.Method.invoke(Method.java:521)                                                  
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)                   
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)                                      
    at dalvik.system.NativeStart.main(Native Method)                                                     
Caused by: java.lang.IllegalStateException: Cursor is closed                                            
    at android.database.sqlite.sqliteCursor.fillWindow(sqliteCursor.java:278)                            
    at android.database.sqlite.sqliteCursor.onMove(sqliteCursor.java:255)                                
    at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:187)                           
    at android.database.CursorWrapper.moveToPosition(CursorWrapper.java:187)                             
    at android.support.v4.widget.CursorAdapter.getItemId(CursorAdapter.java:226)                         
    at android.widget.AbsListView.onSaveInstanceState(AbsListView.java:910)                              
    at android.widget.ListView.onSaveInstanceState(ListView.java:3687)                                   
    at android.view.View.dispatchSaveInstanceState(View.java:6070)                                       
    at android.view.ViewGroup.dispatchFreezeSelfOnly(ViewGroup.java:1197)                                
    at android.widget.AdapterView.dispatchSaveInstanceState(AdapterView.java:759)                        
    at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:1184)                             
    at android.view.View.saveHierarchyState(View.java:6053)                                              
    at android.support.v4.app.FragmentManagerImpl.saveFragmentViewState(FragmentManager.java:1387)       
    at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1439)                
    at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:468)            
    at android.app.Activity.performSaveInstanceState(Activity.java:1040)                                 
    at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1180)            
    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3420)                         
    ... 12 more

而对于好的措施,至少在我的代码中有一行:

android.database.StaleDataException: Access closed cursor                                   
    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:217)
    at android.database.AbstractWindowedCursor.getBlob(AbstractWindowedCursor.java:27)       
    at android.database.CursorWrapper.getBlob(CursorWrapper.java:143)                        
    at android.database.CursorWrapper.getBlob(CursorWrapper.java:143)                        
    at com.testapp.TestFragment$1.setViewValue(TestFragment.java:84)                         
    at android.support.v4.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:131)  
    at android.support.v4.widget.CursorAdapter.getView(CursorAdapter.java:257)               
    at android.widget.AbsListView.obtainView(AbsListView.java:1319)                          
    at android.widget.ListView.makeAndAddView(ListView.java:1789)                            
    at android.widget.ListView.fillDown(ListView.java:656)                                   
    at android.widget.ListView.fillSpecific(ListView.java:1342)                              
    at android.widget.ListView.layoutChildren(ListView.java:1616)                            
    at android.widget.AbsListView.onLayout(AbsListView.java:1172)                            
    at android.view.View.layout(View.java:7037)                                              
    at android.widget.FrameLayout.onLayout(FrameLayout.java:333)                             
    at android.view.View.layout(View.java:7037)                                              
    at android.widget.FrameLayout.onLayout(FrameLayout.java:333)                             
    at android.view.View.layout(View.java:7037)                                              
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)                     
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)                    
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)                          
    at android.view.View.layout(View.java:7037)                                              
    at android.widget.FrameLayout.onLayout(FrameLayout.java:333)                             
    at android.view.View.layout(View.java:7037)                                              
    at android.widget.FrameLayout.onLayout(FrameLayout.java:333)                             
    at android.view.View.layout(View.java:7037)                                              
    at android.view.ViewRoot.performTraversals(ViewRoot.java:1054)                           
    at android.view.ViewRoot.handleMessage(ViewRoot.java:1736)                               
    at android.os.Handler.dispatchMessage(Handler.java:99)                                   
    at android.os.Looper.loop(Looper.java:143)                                               
    at android.app.ActivityThread.main(ActivityThread.java:4717)                             
    at java.lang.reflect.Method.invokeNative(Native Method)                                  
    at java.lang.reflect.Method.invoke(Method.java:521)                                      
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)       
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)                          
    at dalvik.system.NativeStart.main(Native Method)

更新:修正,请看下面我的答案

解决方法

我没有提示问题是什么,但也许这可以帮助你固定下来.

创建一个这样的LoggedCursor并设置光标工厂,这样你的查询就可以给你LoggedCursor:

class LoggedCursor extends sqliteCursor {
    @Override
    public void close() {
        Log.d(TAG,"Cursor closed by:",new RuntimeException("Stack trace"));
        super.close();
    }
}

RuntimeException只是为了轻松记录堆栈跟踪,而不是抛出它.当光标关闭时,您将看到日志中的跟踪.

希望这有助于弄清楚什么时候和由谁关闭.

猜你在找的Android相关文章