0

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"));
    }

}
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
Shinoy Shaji
  • 397
  • 10
  • 27

3 Answers3

3

The iButtonShare object is null. Are you sure you've added this button to your xml layout R.layout.fragment_account_info? And does the button in this layout have the correct id (shareButton)?

Rolf ツ
  • 8,611
  • 6
  • 47
  • 72
sindhu_sp
  • 749
  • 5
  • 12
0

getActivity() in a Fragment returns the Activity the Fragment is currently associated with.

At first add getActivity() instead of getApplicationContext()

       Intent i = new Intent(getActivity(),AccountShareActivity.class);
        startActivity(i);

NullPointerException is thrown when an application attempts to use an object reference that has the null value.

final ImageButton iButtonShare = (ImageButton)view.findViewById(R.id.shareButton);

Make sure that your ImageButton have proper id (R.id.shareButton)

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

Your id is not corrent, here is error

final ImageButton iButtonShare = (ImageButton)view.findViewById(R.id.shareButton);

i think you are using wrong id. also check whether you are using imageView or imageButton

now when you start new activity, try this

Intent i = new Intent(getActivity().getApplicationContext(),AccountShareActivity.class);
        startActivity(i);
Mubashar Javed
  • 749
  • 1
  • 5
  • 12