Search code examples
androidandroid-fragmentsnullpointerexceptiononclicklistener

App Crash occurs on double click on icon listener


I have a toolbar Fragment being called with the property of a left icon listener defined in the Main Fragment(defined below)

Toolbar fragment (common toolbar fragment used everywhere as header)

public void refresh(commonFragment f) {
        if (view != null && f != null && !commonFragment .class.equals(f.getClass()) && !BlankcommonFragment .class.equals(f.getClass())) {

            setRightIcon(f.getRightIcon(), f.getRightIconListener());
            setLeftIcon(f.getLeftIcon(), f.getLeftIconListener());


        }
    }

Common Fragment (common properties kept here for re use)

     public Integer getLeftIcon() {
            return R.drawable.left_fragment_icon;
        }

        public View.OnClickListener getLeftIconListener() {
            return new NavigationOnClickListener(this, new LeftFragment());
        }
 public View.OnClickListener getRightIconListener() {

        return MenuFragment.instance().getDashboardIconListener();
    }

Page Fragment (page where issue occurring on left icon listener)

 @Override
    public Integer getLeftIcon() {
        return R.drawable.back_icon;
    }

    @Override
    public View.OnClickListener getLeftIconListener() {

        return new NavigationOnClickListener(this, new HomeFragment());
    }

NavigationOnClickListener (usage for navigation)

public class NavigationOnClickListener implements View.OnClickListener {
    private Common Fragment from;
    private Common Fragment to;

    public NavigationOnClickListener(Common Fragment from, Common Fragment to) {
        this.from = from;
        this.to = to;
    }

    @Override
    public void onClick(View v) {
        from.navigate(to);
    }
}

back button icon and left icon toggles (same image view but with different id of image) on click on according to the different page.
For example:
There are 3 Fragments: A, B and C, B is the Home Fragment.
By clicking on the left icon on B it takes you to A, and clicking on the back icon on C takes you to B

The issue is double clicking on back icon crashes the app

NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

Error comming on views.setAdapter

    public View build(final LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) { //line added 

         ...

    View home = inflater.inflate(R.layout.fragment_home, container, false);

    final Activity activity = this.getActivity();
    final HomeFragment fragment = this; 

         @Override
        public void onSuccess(Call<List<xyzView>> call, Response<List<xyzView>> response) {
            ListView views = (ListView) container.findViewById(R.id.course_list);
            views.setAdapter(new xyzAdapter(activity, R.layout.fragment_home_item, response.body(), fragment));

How to prevent crash on multiple clicks on back button?


Solution

  • You prevent that nullpointerexception on a view by finding it in the correct XML layout.

    For example, if that listview is in fragment_home.xml...

    View home = inflater.inflate(R.layout.fragment_home, container, false);
    final ListView views = (ListView) home.findViewById(R.id.course_list);
    
    ... 
    
        @Override
        public void onSuccess(Call<List<xyzView>> call, Response<List<xyzView>> response) {
    
            views.setAdapter(new xyzAdapter(getActivity(), R.layout.fragment_home_item, response.body(), HomeFragment.this));