0

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.

Amr Eladawy
  • 4,193
  • 7
  • 34
  • 52
Glenn P
  • 37
  • 8

2 Answers2

0

I assume that ((LinearLayout) linearLayout.getParent()).removeAllViews() removes linearLayout as well. Try doing this:

...
ViewParent parent = linearLayout.getParent();
parent.removeAllViews();
parent.setGravity(Gravity.CENTER);
...
parent.addView(llh);
...
shiftpsh
  • 1,926
  • 17
  • 24
  • Only the dynamic objects under linearlayout (textviews, edittexts) should be removed.The linearlayout refers to a statically created LinearLayout. When I tried the above suggestion, parent.removeAllViews is underlined in red. What could be the reason for this? – Glenn P May 21 '17 at 07:22
  • Then you should call `linearLayout.removeAllViews()` since it removes only child views, not the original one. – shiftpsh May 21 '17 at 07:45
  • I changed the line to: ((LinearLayout)linearLayout).removeAllViews(); but the app still crashis when this line is reached. – Glenn P May 26 '17 at 17:52
0

Here you returning new View that you just created at the end of onCreateView:

return inflater.inflate(R.layout.activity_settings_tab, container, false);

Since you returnning new View non of the Views you found has a valid reference and this cause a NPE. Change it to:

return rootView;
Keivan Esbati
  • 3,376
  • 1
  • 22
  • 36
  • Do you mean like this or did I get it wrong? return inflater.inflate(rootView, container, false);. "rootView" is marked red in this line when I tried. Thanks – Glenn P May 21 '17 at 07:23
  • just I like mentioned above replace entire line with return rootView; you first inflate rootview, then you define views inside it and finally you should return it as your fragment view.. that's why you should return the rootView at the end of your method. – Keivan Esbati May 21 '17 at 07:28
  • Thanks. A newbie question: how can I define views programmatically inside the rootView? Another one: in your answer's last paragraph, can you give a sample code so I would understand what does it mean by "define a public method inside Activity and edit Activity View by calling that method from Fragment." Thanks. – Glenn P May 28 '17 at 16:27
  • Sorry for the last part, I mistook getParent() with getActivity() there is no need for you to define public method in Activity.. Btw did the removeAllViews problem solved? – Keivan Esbati May 28 '17 at 19:20
  • Nope it did not :(. I changed my code to ((LinearLayout) linearLayout).removeAllViews(); however the app is still crashing when it reaches this line. Am I using the public void viewFunds() incorrectly in a fragment? – Glenn P May 29 '17 at 16:03
  • Please attach the full code, the code you attached is a little confusing.. – Keivan Esbati May 29 '17 at 18:29
  • I have already resolved this issue by changing the code to: LinearLayout linearLayout = new LinearLayout(getActivity()); linearLayout.findViewById(R.id.ll_config); linearLayout.removeAllViews(); Thank you. – Glenn P May 31 '17 at 15:38