-1

After being told to use a RecyclerView instead of a ListView, I have tried to implement it along with a SearchView. However, I get a strange error about an object being null:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference

I currently have the following code:

SearchActivity.java

package com.rickteuthof.strangejourneycompendium;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.SearchView;

import java.util.ArrayList;

public class SearchActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {

    public static ArrayList<String> demonNames;
    private SearchAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RecyclerView r = findViewById(R.id.recyclerView);

        demonNames = MainActivity.demonNames;

        adapter = new SearchAdapter(this, demonNames);
        r.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
        r.setAdapter(adapter);

        SearchView s = findViewById(R.id.searchBar);
        s.setOnQueryTextListener(this);

    }

    @Override
    public boolean onQueryTextSubmit(String query) {

        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        adapter.filter(newText);
        return false;
    }
}

SearchAdapter.java

package com.rickteuthof.strangejourneycompendium;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Locale;

public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.MyViewHolder> {

    private LayoutInflater inflater;
    private ArrayList<String> demonNames;
    private ArrayList<String> results;

    public SearchAdapter(Context ctx, ArrayList<String> demonNames){

        inflater = LayoutInflater.from(ctx);
        this.demonNames = demonNames;
        this.results = new ArrayList<String>();
        this.results.addAll(SearchActivity.demonNames);
    }

    @NonNull
    @Override
    public SearchAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = inflater.inflate(R.layout.recycler_item, parent, false);

        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SearchAdapter.MyViewHolder holder, int position) {

        holder.time.setText(demonNames.get(position));
    }

    @Override
    public int getItemCount() {
        return demonNames.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        TextView time;

        public MyViewHolder(View itemView) {
            super(itemView);

            time = itemView.findViewById(R.id.result);
        }

    }

    // Filter Class
    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        SearchActivity.demonNames.clear();
        if (charText.length() == 0) {
            SearchActivity.demonNames.addAll(results);
        } else {
            for (String wp : results) {
                if (wp.toLowerCase(Locale.getDefault()).contains(charText)) {
                    SearchActivity.demonNames.add(wp);
                }
            }
        }
        notifyDataSetChanged();
    }
}

My dataset is a String array named demonNames which I try to filter to get the correct results in the RecyclerView.

Here are my xml layouts:

activity_search.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SearchActivity">

    <SearchView
        android:id="@+id/searchBar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toTopOf="@+id/recyclerView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/searchBar" >

    </android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>

recycler_item.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Rick
  • 31
  • 10

1 Answers1

1

setContentView(R.layout.activity_main);

Replace this with

setContentView(R.layout.activity_search);

Zun
  • 1,553
  • 3
  • 15
  • 26
  • thank you very much, this is indeed the solution, i can accept it in 8 minutes – Rick Jul 10 '18 at 13:53
  • Now that this is fixed, It behaves strangely, Somehow it switches layouts but it's not supposed to do this. It should stay on the same layout as the SearchView and display the results filtered from the dataset. Do you have any idea why this is happening? – Rick Jul 10 '18 at 13:56