Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash while search filtering #103

Closed
jonas-app opened this issue Apr 4, 2016 · 6 comments
Closed

Crash while search filtering #103

jonas-app opened this issue Apr 4, 2016 · 6 comments
Assignees
Labels

Comments

@jonas-app
Copy link

I have an issue that might be caused by misconfiguration.
In our project I use a RecyclerView with three adapters:

  • HeaderAdapter
  • FooterAdapter
  • StickyHeaderAdapter which wraps an custom RecyclerSwipeAdapter

Following libs are used:

  • com.timehop.stickyheadersrecyclerview:library:0.4.3@aar
  • com.daimajia.swipelayout:library:1.2.0@aar'

All but searching is working.
If I type something into the SearchView the app is crashing with the following exception:

java.lang.NullPointerException: Attempt to invoke interface method 'boolean com.mikepenz.fastadapter.IItem.isSelected()' on a null object reference
                                         at com.mikepenz.fastadapter.utils.AdapterUtil.handleStates(AdapterUtil.java:28)
                                         at com.mikepenz.fastadapter.FastAdapter.notifyAdapterItemRangeChanged(FastAdapter.java:1100)
                                         at com.mikepenz.fastadapter.FastAdapter.notifyAdapterItemRangeChanged(FastAdapter.java:1076)
                                         at com.mikepenz.fastadapter.adapters.ItemAdapter.set(ItemAdapter.java:186)
                                         at com.mikepenz.fastadapter.adapters.ItemAdapter$ItemFilter.publishResults(ItemAdapter.java:356)
                                         at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:282)
                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                         at android.os.Looper.loop(Looper.java:135)
                                         at android.app.ActivityThread.main(ActivityThread.java:5254)
                                         at java.lang.reflect.Method.invoke(Native Method)
                                         at java.lang.reflect.Method.invoke(Method.java:372)
                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

In some situations the app is only crashing by specific search caracters ('t'), so I think something is wrong with my filter.
If I return false in the filter, so nothing is filtert, it is working without any issue.
I can't find the root of the problem and want to ask you for help.
Do not hesitate to ask for more details.

Kind Regards,
Jonas Hackenberg

FastAdapter fastAdapter = new FastAdapter();
StickyHeaderAdapter stickyHeaderAdapter = new StickyHeaderAdapter();
HeaderAdapter<HeaderItem> headerAdapter = new HeaderAdapter<>();
RecyclerSwipeAdapter itemAdapter = new RecyclerSwipeAdapter<>();
FooterAdapter<FooterItem> footerAdapter = new FooterAdapter<>();
...
recyclerView.setAdapter(headerAdapter.wrap(footerAdapter.wrap(stickyHeaderAdapter.wrap(itemAdapter.wrap(fastAdapter)))));
...
itemAdapter.withFilterPredicate(new IItemAdapter.Predicate<TestItem>() {
            @Override
            public boolean filter(TestItem item, CharSequence constraint) {
                //return true if we should filter it out
                //return false to keep it
                return !item.provider.toLowerCase().contains(constraint.toString().toLowerCase());
            }
        });

RecyclerSwipeAdapter:

public class RecyclerSwipeAdapter<Item extends IItem> extends ItemAdapter<Item> implements SwipeItemMangerInterface, SwipeAdapterInterface {

    public SwipeItemRecyclerMangerImpl mItemManger = new SwipeItemRecyclerMangerImpl(this);

    @Override
    public int getSwipeLayoutResourceId(int position) {
        return R.id.swipe;
    }

    @Override
    public void openItem(int position) {
        mItemManger.openItem(position);
    }

    @Override
    public void closeItem(int position) {
        mItemManger.closeItem(position);
    }

    @Override
    public void closeAllExcept(SwipeLayout layout) {
        mItemManger.closeAllExcept(layout);
    }

    @Override
    public void closeAllItems() {
        mItemManger.closeAllItems();
    }

    @Override
    public List<Integer> getOpenItems() {
        return mItemManger.getOpenItems();
    }

    @Override
    public List<SwipeLayout> getOpenLayouts() {
        return mItemManger.getOpenLayouts();
    }

    @Override
    public void removeShownLayouts(SwipeLayout layout) {
        mItemManger.removeShownLayouts(layout);
    }

    @Override
    public boolean isOpen(int position) {
        return mItemManger.isOpen(position);
    }

    @Override
    public Attributes.Mode getMode() {
        return mItemManger.getMode();
    }

    @Override
    public void setMode(Attributes.Mode mode) {
        mItemManger.setMode(mode);
    }
}

StickyHeaderAdapter:

public class StickyHeaderAdapter extends AbstractAdapter implements StickyRecyclerHeadersAdapter {
    @Override
    public long getHeaderId(int position) {
        IItem item = getItem(position);

        if (item instanceof TestItem && ((Testtem) item).category != null) {
            return ((TestItem) item).category.position;
        }
        return -1;
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        //we create the view for the header
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_test_category, parent, false);
        return new RecyclerView.ViewHolder(view) {
        };
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
        TextView name = (TextView) holder.itemView.findViewById(R.id.name);
        ImageView icon = (ImageView) holder.itemView.findViewById(R.id.icon);

        IItem item = getItem(position);
        if (item instanceof TestItem && ((TestItem) item).category != null) {
            //based on the position we set the headers text
            name.setText(((TestItem) item).category.name);
            icon.setImageResource(((TestItem) item).category.resource);
        }
    }

    /**
     * REQUIRED FOR THE FastAdapter. Set order to < 0 to tell the FastAdapter he can ignore this one.
     **/

    /**
     * @return
     */
    @Override
    public int getOrder() {
        return -100;
    }

    @Override
    public int getAdapterItemCount() {
        return 0;
    }

    @Override
    public List<IItem> getAdapterItems() {
        return null;
    }

    @Override
    public IItem getAdapterItem(int position) {
        return null;
    }

    @Override
    public int getAdapterPosition(IItem item) {
        return -1;
    }

    @Override
    public int getGlobalPosition(int position) {
        return -1;
    }

}
@mikepenz mikepenz self-assigned this Apr 4, 2016
@mikepenz mikepenz added the bug label Apr 4, 2016
@mikepenz
Copy link
Owner

mikepenz commented Apr 4, 2016

@jonas-arkulpa should be fixed in the next version. The root cause seems that you have a selected item in some cases. The filtering will now deselect all selections before filtering.

@Rainer-Lang
Copy link
Contributor

@mikepenz And what will happen if the filter is empty? Will the selections be restored?

@mikepenz
Copy link
Owner

mikepenz commented Apr 4, 2016

@Rainer-Lang no it won't.

@Rainer-Lang
Copy link
Contributor

That's not "nice". ;)

@jonas-app
Copy link
Author

@mikepenz thanks for the quick reply and fix.

@vinayakmestri
Copy link

vinayakmestri commented May 23, 2018

I have created custom edittext which does not hangs or crash the app.
SearchTimerEditText

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants