unable to get fragment context? showing unreachable code

289 views Asked by At

Unable to get fragment context while running application,showing unreachable code. I have created the DatabaseHelper parameterized constructor with context.

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_direct_search,container,false);

    try {
        myDbHelper.createDataBase(getActivity());
    } catch (IOException e) {
        e.printStackTrace();
    }catch(SQLException sqle){
        throw sqle;
    }


    try {

        myDbHelper.openDataBase();

    }catch(SQLException sqle){

        throw sqle;

    }
    Toast.makeText(getActivity(), "Successfully copied...", Toast.LENGTH_SHORT).show();

    listview=(ListView)view.findViewById(R.id.listView);
    populateListView();
                 return view;
}

this is my DatabaseHelper() constructor in DatabaseHelper Class :

     public DatabaseHelper(Context context) {

    super(context, DB_NAME, null, 1);
    this.myContext = context;
    DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";
}

getting following error while running the application that is unreachable code. error is occuring at the following line :

 myDbHelper.createDataBase(getActivity());

 error : unreachable code
4

There are 4 answers

2
Maximosaic On
return inflater.inflate(R.layout.fragment_direct_search,container,false);

is done quite early in your method, therefore nothing after that will be executed. To fix this change it to:

View view = inflater.inflate(R.layout.fragment_direct_search,container,false);

and put this in the end of your method;

return view;

EDIT:

To properly get your listview you can change then

listview=(ListView)listview.findViewById(R.id.listView);

to

listview=(ListView)view.findViewById(R.id.listView);

and to get the context pass getActivity() instead of this.

3
MHP On

you use return statement twice in your onCreateView method,first in start and second in end,code after first return is unreachable.

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
   // Inflate the layout for this fragment
   return inflater.inflate(R.layout.fragment_direct_search,container,false);// this line should be change
   try {
    myDbHelper.createDataBase();
   } catch (IOException e) {
    e.printStackTrace();
   }catch(SQLException sqle){
    throw sqle;
   }
 .......
}  

change to this:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_direct_search,container,false);
try {
    myDbHelper.createDataBase();
} catch (IOException e) {
    e.printStackTrace();
}catch(SQLException sqle){
    throw sqle;
}


try {

    myDbHelper.openDataBase();

}catch(SQLException sqle){

    throw sqle;

}
Toast.makeText(getActivity(), "Successfully copied...", Toast.LENGTH_SHORT).show();

listview = (ListView) view.findViewById(R.id.listView);//change listview.find... to view.find...
populateListView();
        /*
        c=myDbHelper.query("EMP_TABLE", null, null, null, null,null, null);
        if(c.moveToFirst())
        {
            do {

                Toast.makeText(CopyDbActivity.this,
                        "_id: " + c.getString(0) + "\n" +
                                "E_NAME: " + c.getString(1) + "\n" +
                                "E_AGE: " + c.getString(2) + "\n" +
                                "E_DEPT:  " + c.getString(3),
                        Toast.LENGTH_LONG).show();

            } while (c.moveToNext());
        }

        */
return view;//and finally return view instead of inflate layout again
}
private void populateListView()
{
Cursor c=myDbHelper.getAlldetails();
String[] fromfieldname=new String[]{"NAME","CATEGORY"};
int[] tofield=new int[]{R.id.nameText,R.id.categoryText};

SimpleCursorAdapter simpleCursorAdapter=new SimpleCursorAdapter(getActivity(),R.layout.custom_list_view,c,fromfieldname,tofield);
listview.setAdapter(simpleCursorAdapter);
}
}
2
Vishwa On
Remove this code from first declaration

return inflater.inflate(R.layout.fragment_direct_search,container,false);
and use 
View view=inflater.inflate(R.layout.fragment_direct_search,container,false);
and exit return the view at last.because this function will return the view without process the next line.
0
Trung NT Nguyen On

Please use isAdded() to check if Fragment is added to Activity, then using Handler to check it, in your case:

new Handler().post(new Runnable() {
    @Override
    public void run() {
        if (isAdded()) {
            try {
                myDbHelper.createDataBase();
            } catch (IOException e) {
                e.printStackTrace();
            }catch(SQLException sqle){
            throw sqle;
            }
        }
    }
});