单击搜索按钮后,应用程序崩溃。结果,我无法过滤搜索结果。
这是我的回收站视图
public class RecyclerView_Config {
private Context sContext;
private SupplierAdapter supAdapter;
public void setConfig(RecyclerView recyclerView, Context context, List<supplierDB> suppliers, List<String> keys) {
sContext = context;
supAdapter = new SupplierAdapter(suppliers, keys);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setAdapter(supAdapter);
}
class SupplierView extends RecyclerView.ViewHolder {
private TextView pc, pd, cn, ca, lno, mno, email;
private String key;
public SupplierView(ViewGroup parent) {
super(LayoutInflater.from(sContext).inflate(R.layout.supplier_list_2, parent, false));
pc = (TextView)itemView.findViewById(R.id.pCat_View);
pd = (TextView)itemView.findViewById(R.id.pDesc_View);
cn = (TextView)itemView.findViewById(R.id.cName_View);
ca = (TextView)itemView.findViewById(R.id.cAddr_View);
lno = (TextView)itemView.findViewById(R.id.lNo_View);
mno = (TextView)itemView.findViewById(R.id.mNo_View);
email = (TextView)itemView.findViewById(R.id.eMail_View);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(sContext, viewSupplier.class);
intent.putExtra("key", key);
intent.putExtra("pCategory", pc.getText().toString());
intent.putExtra("pDesc", pd.getText().toString());
intent.putExtra("cName", cn.getText().toString());
intent.putExtra("cAddr", ca.getText().toString());
//intent.putExtra("landline", lno.getText().toString());
//intent.putExtra("mobile", mno.getText().toString());
intent.putExtra("email", email.getText().toString());
sContext.startActivity(intent);
}
});
}
public void bind(supplierDB supplier, String key) {
pc.setText(supplier.getpCategory());
pd.setText(supplier.getpDesc());
cn.setText(supplier.getcName());
ca.setText(supplier.getcAddr());
lno.setText(supplier.getLandline().toString());
mno.setText(supplier.getMobile().toString());
email.setText(supplier.getEmail());
this.key = key;
}
}
class SupplierAdapter extends RecyclerView.Adapter<SupplierView> implements Filterable {
private List<supplierDB> supplierList;
private List<String> keys;
private List<supplierDB> supplierListFull;
public SupplierAdapter(List<supplierDB> supplierList, List<String> keys) {
this.supplierList = supplierList;
this.keys = keys;
supplierListFull = new ArrayList<>(supplierList);
}
@Override
public SupplierView onCreateViewHolder(ViewGroup parent, int viewType) {
return new SupplierView(parent);
}
@Override
public void onBindViewHolder(@NonNull SupplierView holder, int position) {
holder.bind(supplierList.get(position), keys.get(position));
}
@Override
public int getItemCount() {
return supplierList.size();
}
@Override
public Filter getFilter() {
return supplierFilter;
}
private Filter supplierFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
List<supplierDB> filteredList = new ArrayList<>();
if(charSequence == null || charSequence.length() == 0) {
filteredList.addAll(supplierListFull);
} else {
String filterPattern = charSequence.toString().toLowerCase().trim();
for(supplierDB supplier : supplierListFull) {
if(supplier.getcName().toLowerCase().contains(filterPattern)) {
filteredList.add(supplier);
}
}
}
FilterResults results = new FilterResults();
results.values = filteredList;
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
supplierList.clear();
supplierList.addAll((List) filterResults.values);
notifyDataSetChanged();
}
};
}
}
这是我的主要活动
public class Main2Activity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
RecyclerView_Config.SupplierAdapter supAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2activity);
Toolbar tb = findViewById(R.id.tBar);
setSupportActionBar(tb);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, tb, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
if(savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new addSupplier()).commit();
navigationView.setCheckedItem(R.id.nav_add_supplier);
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_add_supplier:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new addSupplier()).commit();
break;
case R.id.nav_view_supplier:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new supplierList()).commit();
break;
//case R.id.nav_view_schedule:
//getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new viewSupplier()).commit();
//break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void onBackPressed() {
if(drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.supplier_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
supAdapter.getFilter().filter(s);
return false;
}
});
return true;
}
}
这是我得到的错误:
java.lang.NullPointerException:尝试调用虚拟方法 'android.widget.Filter com.example.ashvins.suppliermanagement.RecyclerView_Config $ SupplierAdapter.getFilter()' 在空对象引用上