I am new to android development. I transferred code from my MainActivity to a Fragment class. In my onCreateView method, I am calling a viewFunds() method to display data dynamically from SQLite.
However, when I click the tab for that Fragment, the app closes when it reaches the removeAllViews line. I need to removeAllViews first every time this method is called to refresh the data.
Since I am calling ViewFunds() also in other methods for example when deleting data, I call it to refresh the results on the screen.
See code below:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootview = inflater.inflate(R.layout.activity_settings_tab, container, false);
        super.onCreate(savedInstanceState);
        myDb = new DatabaseHelper(getContext());
        btnAddData = (Button) rootview.findViewById(R.id.button_add);
        btnviewAll = (Button) rootview.findViewById(R.id.button_viewAll);
        btnAddFund = (Button) rootview.findViewById(R.id.button_AddFund);
        btnviewUpdate = (Button) rootview.findViewById(R.id.button_update);
        editEmergencyFund = (EditText) rootview.findViewById(R.id.edit_EmergencyFund);
        editProsperityFund = (EditText) rootview.findViewById(R.id.edit_ProsperityFund);
        editRewardFund = (EditText) rootview.findViewById(R.id.edit_RewardFund);
        viewAll(); //this is for a button
        viewFunds();//this is executed when the fragment is called
        return inflater.inflate(R.layout.activity_settings_tab, container, false);
    }
public void viewFunds() { 
        Cursor res2 = myDb.getConfigData();
        res2.moveToFirst();
        final View linearLayout = getActivity().findViewById(R.id.ll_config);
        ((LinearLayout) linearLayout.getParent()).removeAllViews(); //clear layout first - LINE WITH ISSUE
        ((LinearLayout) linearLayout.getParent()).setGravity(Gravity.CENTER);
 //some code follows...
 }
The last line in this group of code that is written after the removeAllViews line also causes the app to close:
 LinearLayout llh = new LinearLayout(getActivity());
            llh.setOrientation(LinearLayout.HORIZONTAL);
            LinearLayout.LayoutParams lp_llh = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            llh.setLayoutParams(lp_llh);
            ((LinearLayout) linearLayout.getParent()).addView(llh); //code  that causes app to close
Thanks in advance for your help.
                        
I assume that
((LinearLayout) linearLayout.getParent()).removeAllViews()removeslinearLayoutas well. Try doing this: