如何尝试在空对象引用上调用接口方法'java.lang.Object java.util.List.remove(int)?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (237)
Main file
public class MainActivity extends AppCompatActivity {

private Toolbar mainToolBar;

private AdView mAdView;

RecyclerView recyclerView;
ProductAdapter adapter;

List<Product> productList;


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




    loadData();



    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);

    recyclerView.setLayoutManager(new LinearLayoutManager(this));



    adapter = new ProductAdapter(this, productList);
    recyclerView.setAdapter(adapter);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.main_menu, menu);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    startActivity(new Intent(MainActivity.this, activity_add.class));


    return true;
}



public void deleteItem(int id){

    id = id - 1;
    productList.remove(id);
    adapter.notifyItemRemoved(id);
}
private void loadData(){
    SharedPreferences sharedPreferences = getSharedPreferences("shared 
    prefs", 0);
    Gson gson = new Gson();
    String json = sharedPreferences.getString("items", null);
    Type type = new TypeToken<ArrayList<Product>>(){}.getType();
    productList = gson.fromJson(json, type);

    if(productList == null){
        productList = new ArrayList<>();
    }
}

private void addToList(){
    SharedPreferences sharedPreferences = getSharedPreferences("shared prefs", 0);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    Gson gson = new Gson();
    String json = gson.toJson(productList);
    editor.putString("items", json);
    editor.apply();
}

other file

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder>{

private Context mCtx;
private List<Product> productList;

public ProductAdapter(Context mCtx, List<Product> productList) {
    this.mCtx = mCtx;
    this.productList = productList;
}

@NonNull
@Override
public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    LayoutInflater inflater = LayoutInflater.from(mCtx);
    View view = inflater.inflate(R.layout.list_layout, null);
    return new ProductViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ProductViewHolder holder, int position) {
    final Product product = productList.get(position);

    holder.textViewTitle.setText(product.getTitle());
    holder.textViewRating.setText(String.valueOf(product.getRating()));



    holder.deleteBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new MainActivity().deleteItem(product.getId());
        }
    });
}

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

class ProductViewHolder extends RecyclerView.ViewHolder{

    TextView textViewTitle, textViewRating;

    Button deleteBtn;

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

        textViewTitle = itemView.findViewById(R.id.textViewTitle);
        textViewRating = itemView.findViewById(R.id.textViewRating);

        deleteBtn = itemView.findViewById(R.id.deleteBtn);

    }
}

}

提问于
用户回答回答于

问题是你正在尝试在MainActivity此处创建新实例:

new MainActivity().deleteItem(product.getId());

MainActivity应该实现一个监听器,然后可以将它传递给ProductAdapter构造函数:

deleteListener.deleteItem(product.getId())

所属标签

可能回答问题的人

  • Hanzo

    6 粉丝0 提问7 回答
  • Richel

    9 粉丝0 提问3 回答
  • 御姐万岁

    6 粉丝507 提问2 回答
  • 用户3845578

    杭州吱吱吱科技 · 站长 (已认证)

    0 粉丝0 提问2 回答

扫码关注云+社区

领取腾讯云代金券