尝试从onScroll隐藏另一个片段时,Listfragment中的异常

时间:2014-05-21 06:10:10

标签: android android-listfragment onscrolllistener

我有一个列表片段,我已经设置了onScrollListenet

dataList.getListView().setOnScrollListener(this);

在Scrool Listener的onScroll内,我有以下代码隐藏片段,如果它可见

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {

    Log.e(TAG,"Inside onScroll"); 
    if(dataDesc.isVisible()){

        Log.e(TAG,"dataDesc Fragment is visible");

    getSupportFragmentManager().openTransaction().hide(dataDesc).commit();
    }
}

并将其显示在onScrollStateChanged中,如下所示

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
    // TODO Auto-generated method stub
    Log.e(TAG,"Inside onScrollStateChanged: scrollState = "+String.valueOf(scrollState));
    if(dataDesc.isHidden()){

        Log.e(TAG,"dataDesc Fragment is hidden");

        getSupportFragmentManager().openTransaction().show(dataDesc).commit();
    }
}

但我得到了异常

05-21 04:34:54.145: E/AndroidRuntime(25146): FATAL EXCEPTION: main
05-21 04:34:54.145: E/AndroidRuntime(25146): java.lang.IllegalStateException: Can not perform this action inside of onLoadFinished
05-21 04:34:54.145: E/AndroidRuntime(25146):    at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1347)
05-21 04:34:54.145: E/AndroidRuntime(25146):    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1361)
05-21 04:34:54.145: E/AndroidRuntime(25146):    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
05-21 04:34:54.145: E/AndroidRuntime(25146):    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
05-21 04:34:54.145: E/AndroidRuntime(25146):    at com.example.test.MainActivity.onScroll(MainActivity.java:664)
05-21 04:34:54.145: E/AndroidRuntime(25146):    at android.widget.AbsListView.invokeOnItemScrollListener(AbsListView.java:1516)
05-21 04:34:54.145: E/AndroidRuntime(25146):    at android.widget.AbsListView.setOnScrollListener(AbsListView.java:1505)
05-21 04:34:54.145: E/AndroidRuntime(25146):    at com.example.test.MainActivity.onListPopulated(MainActivity.java:468)
05-21 04:34:54.145: E/AndroidRuntime(25146):    at com.example.test.DataListFragment.onLoadFinished(DataListFragment.java:280)
05-21 04:34:54.145: E/AndroidRuntime(25146):    at com.example.test.DataListFragment.onLoadFinished(DataListFragment.java:1)
05-21 04:34:54.145: E/AndroidRuntime(25146):    at android.support.v4.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:427)
05-21 04:34:54.145: E/AndroidRuntime(25146):    at android.support.v4.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:395)
05-21 04:34:54.145: E/AndroidRuntime(25146):    at android.support.v4.content.Loader.deliverResult(Loader.java:104)
05-21 04:34:54.145: E/AndroidRuntime(25146):    at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:73)
05-21 04:34:54.145: E/AndroidRuntime(25146):    at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:35)
05-21 04:34:54.145: E/AndroidRuntime(25146):    at android.support.v4.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:223)
05-21 04:34:54.145: E/AndroidRuntime(25146):    at android.support.v4.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:61)
05-21 04:34:54.145: E/AndroidRuntime(25146):    at android.support.v4.content.ModernAsyncTask.finish(ModernAsyncTask.java:461)
05-21 04:34:54.145: E/AndroidRuntime(25146):    at android.support.v4.content.ModernAsyncTask.access$500(ModernAsyncTask.java:47)
05-21 04:34:54.145: E/AndroidRuntime(25146):    at android.support.v4.content.ModernAsyncTask$InternalHandler.handleMessage(ModernAsyncTask.java:474)
05-21 04:34:54.145: E/AndroidRuntime(25146):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 04:34:54.145: E/AndroidRuntime(25146):    at android.os.Looper.loop(Looper.java:137)
05-21 04:34:54.145: E/AndroidRuntime(25146):    at android.app.ActivityThread.main(ActivityThread.java:4898)
05-21 04:34:54.145: E/AndroidRuntime(25146):    at java.lang.reflect.Method.invokeNative(Native Method)
05-21 04:34:54.145: E/AndroidRuntime(25146):    at java.lang.reflect.Method.invoke(Method.java:511)
05-21 04:34:54.145: E/AndroidRuntime(25146):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
05-21 04:34:54.145: E/AndroidRuntime(25146):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
05-21 04:34:54.145: E/AndroidRuntime(25146):    at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案