Search code examples
javaandroidandroid-fragmentsunreachable-statement

Unreachable statement after return using getActivity in a fragment


I'm sorry if someone already asked this, but I keep getting an error with my app. I can't even run because I've got an error saying "Unreachable statement".

Here is my code on my Fragment

package com.example.dasilvadd.students;

public class OngletCours extends Fragment
{

    DatabaseHelper dbhelper = new DatabaseHelper(getActivity());
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.ongletcours, container, false);
        return rootView;

        List<Cours> listeCours;
        ArrayList<String> arrayList;
        ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(),R.layout.ongletcours, arrayList);
        ListView l1= (ListView) getView().findViewById(R.id.ListCours);
        listeCours= dbhelper.getAllCours();
        if (!listeCours.isEmpty())
        {
            String item;
            String[] cours = {""};

            arrayList=new ArrayList<>(Arrays.asList(cours));

            l1.setAdapter(adapter);
            for(int i = 0; i < listeCours.size(); i++) {
                item = listeCours.get(i).getCours();
                arrayList.add(item);

                adapter.notifyDataSetChanged();

            }
        }
        else{
            Toast t = Toast.makeText(getActivity(),"Error",Toast.LENGTH_LONG);
            t.show();
        }

    }

}

When I do getActivity() on my ArrayAdapter, my List<Cours> listeCours; is unreachable, and I don't know why...


Solution

  • You are returning your rootView instance earlier in the function so nothing afterwards will get called. Move

    return rootView;
    

    to the bottom of the function.