fragment button nullpointerexception code example

Example: fragment button nullpointerexception

//You need to call findViewById in your root view instead:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance) {
    View layout = inflater.inflate(R.layout.activity_warrior_self_battle_recyclerview, container, false);
    recyclerView = (RecyclerView) layout.findViewById(R.id.recycler_view);
    LinearLayoutManager layoutManager;


    //change to layout.findViewById():
    bStart = (Button) layout.findViewById(R.id.bOK); //modified
    nP1 = (EditText) layout.findViewById(R.id.nP1); //modified
    nP2 = (EditText) layout.findViewById(R.id.nP2); //modified
    bStart.setOnClickListener(this);

    layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
    recyclerView.setLayoutManager(layoutManager);
    adapter = new WarriorBattleAdapter(getActivity());
    recyclerView.setAdapter(adapter);
    return layout;
}

Tags:

Java Example