我在听众单击按钮以查看详细信息时遇到问题

时间:2018-09-26 09:38:10

标签: android android-cardview recyclerview-layout

这是错误

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

我刚刚了解了android,请指导我,我一直在YouTube上寻找解决方案,但我一直在忙于搜索,但没有找到解决方案

这是mainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private RecyclerView tv_Category;
    private ArrayList<Makanan>list;
    private Button btn_detail;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_Category = (RecyclerView)findViewById(R.id.rv_category);
        tv_Category.setHasFixedSize(true);
        list = new ArrayList<>();
        list.addAll(DataMakanan.getListData());
        showRecyclerCardView();
        btn_detail= (Button)findViewById(R.id.btn_set_detail);
        btn_detail.setOnClickListener(this);
    }

    private void showRecyclerCardView(){
        tv_Category.setLayoutManager(new LinearLayoutManager(this));
        CardViewMakananAdapter cardViewMakananAdapter = new CardViewMakananAdapter(this);
        cardViewMakananAdapter.setListMakanan(list);
        tv_Category.setAdapter(cardViewMakananAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_set_detail:
                Intent moveInstent = new Intent(MainActivity.this, MoveData.class);
                startActivity(moveInstent);
                break;
        }
    }
}

任何帮助将不胜感激。

0 个答案:

没有答案