I have put an on click event for intent to an activity over an ImageButton, which is inside a fragment. But the app crashes with the following error.
Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
AccountFragment.java
public class AccountInfoFragment extends Fragment {
private ArrayList<MyAccountsCard> myAccountsCardData;
public AccountInfoFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_account_info, container, false);
RecyclerView myAccountView=(RecyclerView) view.findViewById(R.id.my_accounts_view);
myAccountView.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL, false));
initializeData();
MyAccountsCardAdapter myAccountsCardAdapter= new MyAccountsCardAdapter(myAccountsCardData);
myAccountView.setAdapter(myAccountsCardAdapter);
final ImageButton iButtonShare = (ImageButton)view.findViewById(R.id.shareButton);
iButtonShare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getContext().getApplicationContext(),AccountShareActivity.class);
startActivity(i);
}
});
return view;
}
private void initializeData(){
myAccountsCardData= new ArrayList<>();
myAccountsCardData.add(new MyAccountsCard("12345678901234","scheme1","current","120000"));
myAccountsCardData.add(new MyAccountsCard("12345678901234","scheme2","savings","5000"));
}
}