evrencoskun / TableView

TableView is a powerful Android library for displaying complex data structures and rendering tabular data composed of rows, columns and cells.
MIT License
3.14k stars 459 forks source link

Exception when the table is being shown #284

Open ehsanTC opened 4 years ago

ehsanTC commented 4 years ago

Hi I have used TableView version 0.8.9 in my app. When the table is starting to show rows, after some rows the below exception is arises:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getLeft()' on a null object reference at com.evrencoskun.tableview.listener.scroll.HorizontalRecyclerViewListener.renewScrollPosition(HorizontalRecyclerViewListener.java:260) at com.evrencoskun.tableview.listener.scroll.HorizontalRecyclerViewListener.onInterceptTouchEvent(HorizontalRecyclerViewListener.java:149) at androidx.recyclerview.widget.RecyclerView.findInterceptingOnItemTouchListener(RecyclerView.java:3151) at androidx.recyclerview.widget.RecyclerView.dispatchToOnItemTouchListeners(RecyclerView.java:3122) at androidx.recyclerview.widget.RecyclerView.onTouchEvent(RecyclerView.java:3283) at android.view.View.dispatchTouchEvent(View.java:9187) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2644) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2351) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2651) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2366) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2651) ....

The layout file for my table cell is just a textview inside a linear layout. Additionally the code for binding the cell is:

@Override
public void onBindCellViewHolder(@NonNull AbstractViewHolder holder, @Nullable Cell cellItemModel, int columnPosition, int rowPosition) {
    MyCellViewHolder viewHolder = (MyCellViewHolder) holder;
    viewHolder.setData(cellItemModel);
}

Do you know where is the problem?

evrencoskun commented 4 years ago

Hi @ehsanTC

To be able the test your case, Could you create this problem in the sample application?

KoTius commented 3 years ago

Unfortunately this happens very often. TableView sometimes does not restore cells when you navigate back from other fragment (on top of fragment with table) or swap between pages in ViewPager, when ViewPager's Page is also fragment with the table. So you return to the fragment with the TableView, it restores RowHeaders, ColumnHeaders, Corner, but not the cells. Cells are empty list. And when you touch that empty list - your crash with

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getLeft()' on a null object reference
        at com.evrencoskun.tableview.listener.scroll.HorizontalRecyclerViewListener.renewScrollPosition(HorizontalRecyclerViewListener.java:260) 

Still can scroll Headers tho.

http://prntscr.com/w0mu8p

We still investigate why exactly this happens and how to fix it

romansolo commented 3 years ago

Hi @ehsanTC @KoTius The issue is related to how the viewpager2 manage with fragments, i.g. when you disable the TabLayoutMediator smoothScroll option the viewpager2 stop to recreate the fragments and the TableView doesn't restore the data, to prevent this behaviour you can call adapter.notifyDataSetChanged() in the fragment onResume() method