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.15k stars 458 forks source link

NullPointerException on CellRecyclerView.getAdapter() #36

Closed Baxtiyor1992 closed 5 years ago

Baxtiyor1992 commented 6 years ago

On version 0.8.5.1 this issue was not, but when I updated library version to 0.8.5.5 I had encountered this problem.

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v7.widget.RecyclerView$Adapter com.evrencoskun.tableview.adapter.recyclerview.CellRecyclerView.getAdapter()' on a null object reference
at com.evrencoskun.tableview.adapter.recyclerview.CellRecyclerViewAdapter.notifyCellDataSetChanged(CellRecyclerViewAdapter.java:173)
at com.evrencoskun.tableview.adapter.AbstractTableAdapter.notifyDataSetChanged(AbstractTableAdapter.java:232)
evrencoskun commented 6 years ago

Hi @Baxtiyor1992 ,

Thank you for your bug report.

The exception does not say much valuable clues. You have probably different configration from sample app. So, you need to find out what makes it.

Maybe you have calling notifyDataSetChangedmethod unnecessarily.

ankitm099 commented 6 years ago

getting same issue, while calling notifyDataSetChanges, I have refreshed the list. so need to call this method???

evrencoskun commented 6 years ago

Hi @cisankit

I want to create the case in my environment. When are you calling this method?

ghost commented 5 years ago

Has this problem been solved? I have the same problem with 0.8.8 version

LunevNF commented 5 years ago

Getting same issue. I have ExpandableListView and table as a child item. So, when I have a new data, I calling notifyDataSetChanges for ExpandableListView and get this exception. Of course, we can recreate ListView, but this is not excelent way

LunevNF commented 5 years ago

No, recreate not helps. This issue happens at getChildView:

2019-05-03 22:08:34.357 9269-9269/ru.tmparts.apz E/AndroidRuntime: FATAL EXCEPTION: main
    Process: ru.tmparts.apz, PID: 9269
    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.evrencoskun.tableview.adapter.recyclerview.CellRecyclerView.isScrollOthers()' on a null object reference
        at com.evrencoskun.tableview.layoutmanager.ColumnLayoutManager.shouldFitColumns(ColumnLayoutManager.java:168)
        at com.evrencoskun.tableview.layoutmanager.ColumnLayoutManager.measureChild(ColumnLayoutManager.java:110)
        at com.evrencoskun.tableview.layoutmanager.ColumnLayoutManager.measureChildWithMargins(ColumnLayoutManager.java:80)
        at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1583)
        at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
        at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
        at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
        at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3336)
        at android.view.View.measure(View.java:23407)
        at android.support.v7.widget.RecyclerView$LayoutManager.measureChildWithMargins(RecyclerView.java:9119)
        at com.evrencoskun.tableview.layoutmanager.CellLayoutManager.measureChildWithMargins(CellLayoutManager.java:400)
        at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1583)
        at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
        at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
        at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
        at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3336)
        at android.view.View.measure(View.java:23407)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6967)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.view.View.measure(View.java:23407)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6967)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
        at android.view.View.measure(View.java:23407)
        at android.widget.ListView.measureScrapChild(ListView.java:1382)
        at android.widget.ListView.measureHeightOfChildren(ListView.java:1450)
        at android.widget.ListView.onMeasure(ListView.java:1355)
        at android.view.View.measure(View.java:23407)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6967)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
        at android.view.View.measure(View.java:23407)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6967)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
        at android.view.View.measure(View.java:23407)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6967)
        at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:401)
        at android.view.View.measure(View.java:23407)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6967)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.view.View.measure(View.java:23407)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6967)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
        at android.view.View.measure(View.java:23407)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6967)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
2019-05-03 22:08:34.358 9269-9269/ru.tmparts.apz E/AndroidRuntime:     at com.android.internal.policy.DecorView.onMeasure(DecorView.java:909)
        at android.view.View.measure(View.java:23407)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2952)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1977)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2236)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1843)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7978)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
        at android.view.Choreographer.doCallbacks(Choreographer.java:723)
        at android.view.Choreographer.doFrame(Choreographer.java:658)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:7000)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
LunevNF commented 5 years ago

Init table in expListView:

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
//        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.acts_child_item, null);
//        }

        List<String> mRowHeaderList = new ArrayList<>();
        List<String> mColumnHeaderList = new ArrayList<>();
        List<List<String>> mCellList = new ArrayList<>();

        //отобразить данные
        TableView table_ = convertView.findViewById(R.id.table_);

        //Формируем шапку таблицы
        mColumnHeaderList.add(items.get(groupPosition).getHead().getClientName());
        mColumnHeaderList.add(items.get(groupPosition).getHead().getContractName());
        mColumnHeaderList.add(items.get(groupPosition).getHead().getContractID());
        mColumnHeaderList.add(items.get(groupPosition).getHead().getDays());
        mColumnHeaderList.add(items.get(groupPosition).getHead().getStatus());

        //Добавление строк с ячейками
        for (LineReport15 line : items.get(groupPosition).getLines()) {
            List<String> row = new ArrayList<>();
            row.add(line.getClientName());
            row.add(line.getContractName());
            row.add(line.getContractID());
            row.add(Integer.toString(line.getDays()));
            row.add(line.getStatus());
            mCellList.add(row);

            //Формируем ячейки для зафиксированного столбца
            //mRowHeaderList.add(line.getName());
        }

        //Создание таблицы
        ActsAdapter adapter = new ActsAdapter(context);
        table_.setAdapter(adapter);
        adapter.setAllItems(mColumnHeaderList, mRowHeaderList, mCellList);

        return convertView;
    }

In xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="4dp"
    >

    <com.evrencoskun.tableview.TableView
        android:id="@+id/table_"
        android:layout_marginTop="8dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:nestedScrollingEnabled="true"

        app:column_header_height="66dp"
        app:row_header_width="0dp"
        app:selected_color="@drawable/list_selector"
        app:shadow_color="#A5A5A5"
        app:unselected_color="#ffffff"
        />

</LinearLayout>
LunevNF commented 5 years ago

@evrencoskun You can close this issue. I found the reason: we need to wait until table be created (in my case I need to wait some mseconds after getting child view of expListView). We try to set items, but table not created yet.

So, I just add to getChildView:

    //Создание таблицы
    final ActsAdapter adapter = new ActsAdapter(context);
    table_.setAdapter(adapter);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            adapter.setAllItems(mColumnHeaderList, mRowHeaderList, mCellList);
        }
    }, 200);