我正在对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 / Android运行时:致命异常:主要 流程:com.qasoftearth.abcipl,PID:22202 java.lang.RuntimeException:无法启动活动ComponentInfo {com.qasoftearth.abcipl / com.qasoftearth.abcipl.NavDrawerActivity}: 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) 在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) 在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(NavDrawerActivity.java:288) 在com.qasoftearth.abcipl.NavDrawerActivity.access $ 100(NavDrawerActivity.java:60) 在com.qasoftearth.abcipl.NavDrawerActivity $ 2.afterTextChanged(NavDrawerActivity.java:168) 在android.widget.TextView.sendAfterTextChanged(TextView.java:8356) 在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) 在android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3264) 在android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3264) 在android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3264) 在android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3264) 在android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3264) 在android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3264) 在android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.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(Activity.java:979) 在android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1171) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2551) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2666) 在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) 在com.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();
}
}
答案 0 :(得分:0)
原因是Android生命周期。
活动还原> TextView还原> TextView setText> afterTextChanged
答案 1 :(得分:0)
在致电
之前,请确保已初始化productAdapter
productAdapter.filterList(filteredNames);
像您的代码中的 productAdapter
在获取json之后分配值,但是search_edittext afterTextChanged
的{{1}}会在此之前调用search_edittext上的空文本。这就是它引发NullPointerException的原因。
添加支票
TextChangedListener
答案 2 :(得分:0)
第一件事。调用未首先初始化的对象时,您将获得NullPointerException。因此,在您的代码中,当您从网络调用(这是NavDrawerActivity中的“ fetchJSON”方法)收到响应时,可以看到您正在初始化适配器。因此,您可以通过两种方式解决此问题,
在活动的onCreate中初始化ProductAdapter,并且当您从网络调用(在fetchJSON方法中)获得响应时,只需通过调用productAdapter.notifyDatasetChanged();
> li>在afterTextChanged中使用if条件,您将在其中检查适配器是否已初始化。例如
if(productAdapter!= null){ filter(editable.toString()); }