在片段中使用RecyclerView导致应用崩溃,行中没有错误

时间:2019-11-16 07:31:22

标签: java android android-fragments android-recyclerview

我在我的应用程序上使用了recyclerView来显示数组项。在我对其进行更改之前,它在“ MainActivity.java”中实现时效果很好。

我希望我的应用具有“ BottomNavigationBar” 但是,当我启动该应用程序时,它只是崩溃了

这是片段代码:

public class HomeFragment extends Fragment {

    private RecyclerView rvProduk;
    private ArrayList<Produk> list = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_home, container, false);
        rvProduk = v.findViewById(R.id.rv_produk);
        rvProduk.setHasFixedSize(true);
        list.addAll(ProdukData.getListData());
        showRecyclerView();
        return v;
    }

    private void showRecyclerView(){
        rvProduk.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        ProdukAdapter produkAdapter = new ProdukAdapter();
        rvProduk.setAdapter(produkAdapter);
    }
}

这是MainActivity:

public class MainActivity extends AppCompatActivity {

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

        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
        bottomNavigationView.setOnNavigationItemReselectedListener((BottomNavigationView.OnNavigationItemSelectedListener) navListener);
    }

    private BottomNavigationView.OnNavigationItemReselectedListener navListener =
            new BottomNavigationView.OnNavigationItemReselectedListener() {
                @Override
                public void onNavigationItemReselected(@NonNull MenuItem menuItem) {
                    Fragment selectedFragment = new HomeFragment();

                    switch (menuItem.getItemId()) {
                        case R.id.nav_home:
                            selectedFragment = new HomeFragment();
                            break;
                        case R.id.nav_feed:
                            selectedFragment = new FeedFragment();
                            break;
                        case R.id.nav_profile:
                            selectedFragment = new ProfileFragment();
                            break;
                    }

                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                            selectedFragment).commit();
                }
            };
}

和适配器:

public class ProdukAdapter extends RecyclerView.Adapter<ProdukAdapter.ProdukViewHolder> {
    private ArrayList<Produk> listProduk;

    @NonNull
    @Override
    public ProdukViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_cardview_produk, viewGroup, false);
        return new ProdukViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ProdukViewHolder holder, final int position) {
        final Produk produk = listProduk.get(position);

        Glide.with(holder.itemView.getContext())
                .load(produk.getPhoto())
                .apply(new RequestOptions().override(350, 550))
                .into(holder.imgPhoto);

        holder.tvName.setText(produk.getName());
        holder.tvDetail.setText(produk.getDetail());
        holder.btnDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent moveIntent = new Intent(holder.itemView.getContext(), DetailActivity.class);
                moveIntent.putExtra(DetailActivity.PRODUK_NAME, produk.getName());
                moveIntent.putExtra(DetailActivity.PRODUK_DETAIL, produk.getDetail());
                moveIntent.putExtra("photo", listProduk.get(position).getPhoto());
                holder.itemView.getContext().startActivity(moveIntent);
            }
        });
    }

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

    public class ProdukViewHolder extends RecyclerView.ViewHolder {
        ImageView imgPhoto;
        TextView tvName, tvDetail;
        Button btnBuy, btnDetail;

        ProdukViewHolder(View itemView){
            super(itemView);
            imgPhoto = itemView.findViewById(R.id.img_item_photo);
            tvName = itemView.findViewById(R.id.tv_item_name);
            tvDetail = itemView.findViewById(R.id.tv_item_detail);
            btnBuy = itemView.findViewById(R.id.btn_set_buy);
            btnDetail = itemView.findViewById(R.id.btn_set_detail);
        }
    }
}

有人可以帮我吗? 我还在android上学习

1 个答案:

答案 0 :(得分:1)

您尚未发送任何数据到ProdukAdapter。您应该在ProdukAdapter中创建一个构造函数并传递您的列表。我认为您在代码中得到了NullPointerException。顺便说一句,您应该始终与诸如此类的问题共享日志。

尝试如下更改代码

片段

private void showRecyclerView(){

rvProduk.setLayoutManager(new LinearLayoutManager(this.getActivity()));
 ProdukAdapter produkAdapter = new ProdukAdapter(list);
 rvProduk.setAdapter(produkAdapter); 
}

适配器

public class ProdukAdapter extends RecyclerView.Adapter<ProdukAdapter.ProdukViewHolder> {

 private ArrayList<Produk> listProduk; 

public ProdukAdapter(ArrayList<Produk> listProduk)
{
this.listProduk=listProduk
}

................

}