顯示具有 rotate 標籤的文章。 顯示所有文章
顯示具有 rotate 標籤的文章。 顯示所有文章

2016年8月11日 星期四

after rotate the spinder can’t control the fragment

after rotate, the spider can’t control the fragment

it is because the reference to the fragment lost after rotate,(the activity create again,show lost),but the fragment still here,we just need to find it bu tha

I find the the savedInstanceState auto,so we can

use  if (savedInstanceState == null) {

           mainFragment = new MainFragment();
           Logger.d("create a mainFragment");
           fragmentTransaction.add(R.id.frame_layout_main, mainFragment);
           fragmentTransaction.commit();

       }


and find that mainFragment is null after rotate,so we need to get it back

why after rotate ,will have error ?

why after rotate ,will have error  ?

FATAL EXCEPTION: main
                                                                           Process: pom2.poly.com.trythemoviedbapi, PID: 798
                                                                           java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT * FROM movie
                                                                               at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
                                                                               at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:58)
                                                                               at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:151)
                                                                               at android.database.sqlite.SQLiteCursor.onMove(SQLiteCursor.java:123)
                                                                               at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:236)
                                                                               at android.database.CursorWrapper.moveToPosition(CursorWrapper.java:197)
                                                                               at pom2.poly.com.trythemoviedbapi.MyRecyclerViewAdapter.onBindViewHolder(MyRecyclerViewAdapter.java:57)
                                                                               at pom2.poly.com.trythemoviedbapi.MyRecyclerViewAdapter.onBindViewHolder(MyRecyclerViewAdapter.java:18)
                                                                               at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5212)
                                                                               at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5245)


it is because the program want to mcursor.moveToPosition(position);,However, the mcursor already is coled. But why the cursor closed? the cursor get from the cursor loder should be kepp alive.No, it is not keep alive,  it jsut say after rotate,it will go to onLoadFinished directly.

after rotate,the onLoadFinished bennig call few time,so the myrecycleViewadapter.swapCursor(data); call too,

at here,may be ssome cursor is closed,so,have error,

soultion:
Method1.
Before use the cursor,check it it clise,,  !mcursor.isClosed()

Method 2

why the onLoadFinished call few time? one reson is it will call by MainActivity and MainFragment itself,Total 10