-5

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...

Sufian
  • 6,405
  • 16
  • 66
  • 120
David Silva
  • 67
  • 1
  • 11

1 Answers1

1

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.

Alvin Abia
  • 1,221
  • 1
  • 17
  • 18
  • I'm getting another error now.. ArrayList arrayList ; ArrayAdapter adapter = new ArrayAdapter(getActivity(),R.layout.ongletcours, arrayList); It says that my Arraylist might not vahe ben initialized And thank you for your help for the return ! – David Silva Feb 14 '17 at 12:54
  • ListView l1 = (ListView)rootView.findViewById(R.id.ListCours); – Alvin Abia Feb 14 '17 at 13:11
  • Everything is working, thank you a lot !!! – David Silva Feb 14 '17 at 13:41