Я использую фильтр поиска для recyclerview.Мое приложение автоматически падает через некоторое время, похоже, что текстовый наблюдатель вызывает автоматически.
Я получаю эту ошибку
> Caused by: java.lang.NullPointerException: Attempt to invoke virtual
> method 'void
> com.qasoftearth.abcipl.Adapter.ProductAdapter.filterList(java.util.ArrayList)'
> on a null object reference
> at com.qasoftearth.abcipl.NavDrawerActivity.filter(NavDrawerActivity.java:288)
> at com.qasoftearth.abcipl.NavDrawerActivity.access$100(NavDrawerActivity.java:60)
> at com.qasoftearth.abcipl.NavDrawerActivity$2.afterTextChanged(NavDrawerActivity.java:168)
**
E / AndroidRuntime: ИСКЛЮЧИТЕЛЬНОЕ ИСКЛЮЧЕНИЕ: основной процесс: com.qasoftearth.abcipl, PID: 22202 java.lang.RuntimeException: Невозможно запустить действие ComponentInfo {com.qasoftearth.abcipl / com.qasoftearth.abcipl.NavDraw: java.lang.NullPointerException: попытка вызвать виртуальный метод void com.qasoftearth.abcipl.Adapter.ProductAdapter.filterList (java.util.ArrayList) 'для ссылки на пустой объект в android.app.ActivityThread.performLaunchActivity (ActivityThread.java): 2584) на android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2666) на android.app.ActivityThread.handleRelaunchActivity (ActivityThread.java:4336) на android.app.ActivityThread.-wrap15 (ActivityThread.java) на андроиде.app.ActivityThread $ H.handleMessage (ActivityThread.java:1499) на android.os.HandlerФайлСобственный метод) на com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:789) на com.android.internal.os.ZygoteInit.main (ZygoteInit.java:679). Вызывается: java.lang.NullPointerException: попытка вызвать виртуальный метод void com.qasoftearth.abcipl.Adapter.ProductAdapter.filterList (java.util.ArrayList) 'для ссылки на пустой объект в com.qasoftearth.abcipl.NavDrawerActivity.filter (NavDrawerA288).на com.qasoftearth.abcipl.NavDrawerActivity.access $ 100 (NavDrawerActivity.java:60) на com.qasoftearth.abcipl.NavDrawerActivity $ 2.afterTextChanged (NavDrawerActivity.java:168) на android.widget.javaTextTextText) в android.widget.TextView.setText (TextView.java:4419) в android.widget.TextView.setText (TextView.java:4252) в android.widget.EditText.setText (EditText.java: 90) в android.widget.TextView.setText (TextView.java:4227) в android.widget.TextView.onRestoreInstanceState (TextView.java:4120) в android.view.View.dispatchRestoreInstanceState (View.java:14957 at)android.view.ViewGroup.dispatchRestoreInstanceState (ViewGroup.java:3264) в android.view.ViewGroup.dispatchRestoreInstanceState (ViewGroup.java:3264) в android.view.ViewGroup.dispatchRestoreInstanceState (ViewGroup.jroup.roid.6464).dispatchRestoreInstanceState (ViewGroup.java:3264) в android.view.ViewGroup.dispatchRestoreInstanceState (ViewGroup.java:3264) в android.view.ViewGroup.dispatchRestoreInstanceState (ViewGroup.java:3264) .group.torest.ist.Java: 3264) в android.view.View.restoreHierarchyState (View.java:14935) в com.android.internal.policy.PhoneWindow.restoreHierarchyState (PhoneWindow.java:2123) в android.app.Activity.onRestoreInstanceState (Activity.java: 1024) в android.app.Activity.performRestoreInstanceState (Acttivation.java:979) в android.app.Instrumentation.callActivityOnRestoreInstanceState (Instrumentation.java:1171) в android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2551) в android.app.ActivityThread.handleLaunchActivity (ActivityThread.java)) в android.app.ActivityThread.handleRelaunchActivity (ActivityThread.java:4336) в android.app.ActivityThread.-wrap15 (ActivityThread.java)на android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1499) на android.os.Handler.dispatchMessage (Handler.java:111) на android.os.Looper.loop (Looper.java:207) на android.app.ActivityThread.main (ActivityThread.java:5769) на java.lang.reflect.Method.invoke (собственный метод) на com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:789) на ком.android.internal.os.ZygoteInit.main (ZygoteInit.java:679)
**
NavDrawerActivity.java
public class NavDrawerActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
EditText search_edittext;
TextView viewCart,orderNow,addToCart,txtItemName,txtItemModelNum,txtItemDescriptions;
RecyclerView recyclerViewCategory,recyclerViewProduct;
ArrayList<CategoryDataModel> categoryArrayList;
ArrayList<ProductModelClass> productModelClassList;
ProductAdapter productAdapter;
CategoryAdapter categoryAdapter;
IntentFilter intentFilter;
MyReceiver receiver;
ShimmerFrameLayout shimmerContainer;
SharedPrefLogin sharedPrefLogout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nav_drawer);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
sharedPrefLogout=new SharedPrefLogin(getApplicationContext());
categoryArrayList=new ArrayList<>();
productModelClassList=new ArrayList<>();
recyclerViewCategory = findViewById(R.id.recycler_view_horizontal);
recyclerViewProduct = findViewById(R.id.recycler_view_product_list);
search_edittext=findViewById(R.id.search_edittext);
viewCart=findViewById(R.id.txtv_viewCart);
orderNow=findViewById(R.id.txtv_OrderNow);
addToCart=findViewById(R.id.txtvAddToCart);
txtItemName=findViewById(R.id.txtItemName);
txtItemModelNum=findViewById(R.id.txtItemModelNum);
txtItemDescriptions=findViewById(R.id.txtItemDescriptions);
shimmerContainer = findViewById(R.id.shimmer_view_container);
shimmerContainer.startShimmerAnimation();
intentFilter = new IntentFilter();
intentFilter.addAction(Constants.CONNECTIVITY_ACTION);
if(InternetUtils.checkForInternet(getApplicationContext()))
{
fetchJSON();
}
else
{
if (!InternetUtils.checkForInternet(getApplicationContext())) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("No Internet Connection");
alertDialogBuilder
.setMessage("Please check your internet connection. ")
.setCancelable(false).setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
receiver = new MyReceiver();
registerReceiver(receiver, intentFilter);
}
//adding a TextChangedListener
//to call a method whenever there is some change on the EditText
search_edittext.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable editable) {
//after the change calling the method and passing the search input
filter(editable.toString());
}
});
search_edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
search_edittext.clearFocus();
InputMethodManager in = (InputMethodManager)getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(search_edittext.getWindowToken(), 0);
return true;
}
return false;
}
});
viewCart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent viewCart=new Intent(NavDrawerActivity.this,CartListActivity.class);
startActivity(viewCart);
}
});
//*************************************Recyclerview Category**********************************************************
recyclerViewCategory.setHasFixedSize(true);
categoryArrayList.add(new CategoryDataModel("All Categories",R.drawable.delhi));
categoryArrayList.add(new CategoryDataModel("Fashion",R.drawable.delhi));
categoryArrayList.add(new CategoryDataModel("Mobile and \nElectronics",R.drawable.delhi));
categoryArrayList.add(new CategoryDataModel("Home and \n Living",R.drawable.delhi));
categoryArrayList.add(new CategoryDataModel("Daily Needs",R.drawable.delhi));
categoryArrayList.add(new CategoryDataModel("Books",R.drawable.delhi));
recyclerViewCategory.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false));
categoryAdapter = new CategoryAdapter(this, categoryArrayList);
recyclerViewCategory.setAdapter(categoryAdapter);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onResume() {
super.onResume();
shimmerContainer.startShimmerAnimation();
}
@Override
public void onPause() {
shimmerContainer.stopShimmerAnimation();
super.onPause();
}
private class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Constants.CONNECTIVITY_ACTION)){
if(InternetUtils.checkForInternet(context))
{
fetchJSON();
unregisterReceiver(receiver);
}
}
}
}
private void filter(String text) {
//new array list that will hold the filtered data
ArrayList<ProductModelClass> filteredNames = new ArrayList<>();
//looping through existing elements
for (ProductModelClass s : productModelClassList) {
//if the existing elements contains the search input
if (s.getName().toLowerCase().contains(text.toLowerCase())) {
//adding the element to filtered list
filteredNames.add(s);
}
}
Log.d("wwwee",text);
//calling a method of the adapter class and passing the filtered list
productAdapter.filterList(filteredNames);
}
private void fetchJSON() {
//creating a string request to send request to the url
StringRequest stringRequest = new StringRequest(Request.Method.GET, URLs.jsonURL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("tennis", response.toString());
try {
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.getString("status").equals("true")) {
JSONArray dataArray = jsonObject.getJSONArray("data");
for (int i = 0; i < dataArray.length(); i++) {
ProductModelClass playersModel = new ProductModelClass();
JSONObject dataobj = dataArray.getJSONObject(i);
playersModel.setId(dataobj.getInt("id"));
playersModel.setName(dataobj.getString("name"));
playersModel.setCountry(dataobj.getString("country"));
playersModel.setCity(dataobj.getString("city"));
playersModel.setImgURL(dataobj.getString("imgURL"));
productModelClassList.add(playersModel);
}
}
recyclerViewProduct.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
productAdapter= new ProductAdapter(getApplicationContext(),productModelClassList);
recyclerViewProduct.setAdapter(productAdapter);
shimmerContainer.stopShimmerAnimation();
shimmerContainer.setVisibility(View.GONE);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//displaying the error in toast if occurrs
// Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
//Toast.makeText(getApplicationContext(), "No Internet Connection", Toast.LENGTH_SHORT).show();
}
});
//creating a request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//adding the string request to request queue
requestQueue.add(stringRequest);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.nav_drawer, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
/* if (id == R.id.action_settings) {
return true;
}*/
if (id == R.id.action_cart) {
startActivity(new Intent(NavDrawerActivity.this, CartListActivity.class));
return true;
}
if (id == R.id.action_settings) {
startActivity(new Intent(NavDrawerActivity.this, SettingsActivity.class));
return true;
}
if (id == R.id.action_logout) {
sharedPrefLogout.logout();
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
FragmentManager fragmentManager=getSupportFragmentManager();
if (id == R.id.nav_about) {
// fragmentManager.beginTransaction().replace(R.id.content_frame,new Menu1Fragment()).commit();
Intent intent=new Intent(NavDrawerActivity.this, AboutUsActivity.class);
startActivity(intent);
}
if (id == R.id.nav_user) {
setTitle("Home");
// fragmentManager.beginTransaction().replace(R.id.content_frame,new Menu1Fragment()).commit();
Intent intent=new Intent(NavDrawerActivity.this, UserProfileActivity.class);
startActivity(intent);
} else if (id == R.id.nav_uploadImages) {
//fragmentManager.beginTransaction().replace(R.id.content_frame,new Menu1Fragment()).commit();
Intent intent=new Intent(NavDrawerActivity.this, UploadImagesActivity.class);
startActivity(intent);
// fragmentManager.beginTransaction().replace(R.id.content_frame,new Menu2Fragment()).commit();
} else if (id == R.id.nav_order_history) {
Intent intent=new Intent(NavDrawerActivity.this, OrderHistoryTabActivity.class);
startActivity(intent);
} else if (id == R.id.nav_settings) {
Intent intent=new Intent(NavDrawerActivity.this,SettingsActivity.class);
startActivity(intent);
} else if (id == R.id.nav_share) {
shareIt();
}
else if (id == R.id.nav_helpdesk) {
Intent intent=new Intent(NavDrawerActivity.this, HelpdeskActivity.class);
startActivity(intent);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
ProductAdapter.java
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {
private Context ctx;
private List<ProductModelClass> productModelClassList;
public ProductAdapter(Context ctx, List<ProductModelClass> productModelClassList) {
this.ctx = ctx;
this.productModelClassList = productModelClassList;
}
@NonNull
@Override
public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
LayoutInflater inflater=LayoutInflater.from(ctx);
View view=inflater.inflate(R.layout.layout_product_item_list,null);
return new ProductViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ProductViewHolder holder, final int position) {
Picasso.get().load(productModelClassList.get(position).getImgURL()).into(holder.imageView_product);
holder.txtview_itemname.setText(productModelClassList.get(position).getName());
holder.txtview_model.setText(productModelClassList.get(position).getCountry());
holder.txtview_desc.setText(productModelClassList.get(position).getCity());
holder.editTextQuantity.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
holder.editTextQuantity.setHint("");
else
holder.editTextQuantity.setHint("quantity");
}
});
holder.addToCart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ProductModelClass item = new ProductModelClass();
int pid= productModelClassList.get(position).getId();
String image=productModelClassList.get(position).getImgURL();
String name = holder.txtview_itemname.getText().toString();
String country = holder.txtview_model.getText().toString();
String city = holder.txtview_desc.getText().toString();
ProductListDBHelper dbHelper = new ProductListDBHelper(ctx.getApplicationContext());
SQLiteDatabase database = dbHelper.getWritableDatabase();
dbHelper.insertData(pid,image,name,country,city,database);
Toast.makeText(ctx.getApplicationContext(), "Product Added to the Cart", Toast.LENGTH_SHORT).show();
dbHelper.close();
}
});
}
@Override
public int getItemCount() {
return productModelClassList.size();
}
public class ProductViewHolder extends RecyclerView.ViewHolder {
EditText editTextQuantity;
ImageView imageView_product;
TextView txtview_itemname,txtview_model,txtview_desc,addToCart;
Spinner spinner;
public ProductViewHolder(@NonNull View itemView) {
super(itemView);
imageView_product=itemView.findViewById(R.id.imageview_product);
txtview_itemname=itemView.findViewById(R.id.txtItemName);
txtview_model=itemView.findViewById(R.id.txtItemModelNum);
txtview_desc=itemView.findViewById(R.id.txtItemDescriptions);
editTextQuantity=itemView.findViewById(R.id.edttxt_quantity);
addToCart=itemView.findViewById(R.id.txtvAddToCart);
// spinner=(Spinner)itemView.findViewById(R.id.spinnerQuantity);
}
}
public void filterList(ArrayList<ProductModelClass> filterdNames) {
this.productModelClassList = filterdNames;
Log.d("dlkd", String.valueOf(productModelClassList));
notifyDataSetChanged();
}
}