[Solved] How to replace setContentView in a fragment for SQLdatabase?


First, delete this from your onCreate():

setContentView(R.layout.fragment_section_number1);

Next, move the following from your onCreate():

myDb = new DatabaseHelper(this);
editName = (EditText) rootview.findViewById(R.id.editText_Name);
editSurname = (EditText) rootview.findViewById(R.id.editText_Surname);
editBalance = (EditText) rootview.findViewById(R.id.editText_Balance);
btnAddData = (Button) rootview.findViewById(R.id.button_add);
btnviewAll = (Button) rootview.findViewById(R.id.button_viewAll);
AddData();
viewAll();

…to your onCreateView() method:

rootview = inflater.inflate(R.layout.fragment_section_number1, container, false);
// move it here
return rootview;

EDIT: I forgot to point out that you’re also referencing your views incorrectly.

Instead of this:

editName = (EditText) editName.findViewById(R.id.editText_Name);

It should be this:

editName = (EditText) rootView.findViewById(R.id.editText_Name);

All your views in this case must be referenced from the view that contains them, which is the view you’ve named “rootView”… rootView.findViewById(R.id.name_of_your_view);

4

solved How to replace setContentView in a fragment for SQLdatabase?