0

I have the following problem in code I create fragment and that is OK.
My problem is when I try to set him and parameter.
Here is my code.

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        0, ViewGroup.LayoutParams.WRAP_CONTENT);
params.weight = 1.0f;

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FragmentImageList myFragment = new FragmentImageList();

fragmentTransaction.add(631, myFragment, "fragmentTag");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

myFragment.getView().setLayoutParams(params);

And then in the console I have the following error.

Attempt to invoke virtual method 'void android.view.View.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference

In which makes sense because view of Fragment is not yet been created. But how to do it to set "Params" of created Fragment.

OneByte
  • 101
  • 1
  • 5

3 Answers3

1

631 which you are passing to:

  fragmentTransaction.add(631, myFragment, "fragmentTag");  

should be a view id on which you want to add fragment. Make sure that 631 is id of that view, otherwise pass like R.id.yourViewId

Rohit Sharma
  • 2,017
  • 1
  • 20
  • 22
  • Because before that I create LinearLayout in code and this is his ID. The aim is to create a grid of rows and columns without GridLayout. – OneByte Jan 24 '16 at 16:29
0

Thanks for the answers, I feel stupid.
My goal was to create a grid with Fragment.
But this whole scheme with parameter was completely unnecessary.

@Hitesh Sahu, thank you again.
Тhat part of your answer was key.

Fragments are like placeholders they automatically fill their holding container view (eg. FrameLyout )and you do not need to assign their width and height manually.


Simply could to create another res/layout file to use it in the fragment.
Only for this grid :)

OneByte
  • 101
  • 1
  • 5
-1

I dont know what are your requirements but I think you should be inflating Views instead of Fragment.

Fragments are like placeholders they automatically fill their holding container view (eg. FrameLyout )and you do not need to assign their width and height manually. But if you still want to do so, you should be doing it in onCreate View in your Fragment class

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            rootView = inflater.inflate(R.layout.home_fragment, container, false);

 // assign new layout params on rootView here

------------------------------------------OR------------------------------------------------------------------

Use layout inflater to do the same

       //Use Layout inflater
        LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        //Load layout xml of child layout you want to add
        View childView= inflater.inflate(R.layout.viewpager_place_holder, null);


        //Get and Assign values on views of child view
       ((TextView) childView.findViewById(R.id.pager_title))
                            .setText("Test");

       // Set width and margin on child layout
       LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);

        layoutParams.setMargins(
                            UtilFunctions.getUtilFunctions().dpToPixels(5,
                                    context.getResources()),
                            UtilFunctions.getUtilFunctions().dpToPixels(5,
                                    context.getResources()),
                            UtilFunctions.getUtilFunctions().dpToPixels(5,
                                    context.getResources()),
                            UtilFunctions.getUtilFunctions().dpToPixels(5,
                                    context.getResources()));

      //Add child to the root layout
      rootLayout.addView(childView, layoutParams);
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154