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
is done quite early in your method, therefore nothing after that will be executed. To fix this change it to:
and put this in the end of your method;
EDIT:
To properly get your listview you can change then
to
and to get the context pass
getActivity()
instead ofthis
.