简体   繁体   中英

Reference a programmatically added Framelayout (Android)

I'm new to android development so this might be pretty straightforward. However, I couldn't find anything that worked for me.

I've managed to successfully add multiple framelayouts to a linear layout through the following code:

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    LinearLayout fragmentContainer=(LinearLayout) findViewById(R.id.fragment_container);
    fragmentContainer.setOrientation(LinearLayout.VERTICAL);


    //if (findViewById(R.id.fragment_container) != null) {

    for(int i=1;i<10;i++){
        //create a new TextFrag object
        TextFrag textFragment=new TextFrag();

        //pass arguments to the textFrag Object with the Fragment Number
        Bundle args=new Bundle();
        args.putInt("FragNumber", i);
        textFragment.setArguments(args);

        //Create a new Frame Layout for Each Fragment. This is useful if you want to switch fragments at runtime
        //Dont know exact details yet
        FrameLayout fragmentFrame=new FrameLayout(this);
        //set a unique id for the FrameLayout
        fragmentFrame.setId(i); 

        //Define the Frame Layout Params
        FrameLayout.LayoutParams params=new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);




        //Add the Fragment to the Frame Layout
        getSupportFragmentManager().beginTransaction().add(fragmentFrame.getId(),textFragment).commit();



        //Add the Frame Layout to the Linear Layout
        fragmentContainer.addView(fragmentFrame);


    }



}

So at some point later in the program I'd like to reference these framelayouts and I figured since I knew the IDs I could just use getViewById.

This doesn't seem to work though. I've only slightly modified the above code by adding a redundant FrameLayout object called testFrame as below:

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    LinearLayout fragmentContainer=(LinearLayout) findViewById(R.id.fragment_container);
    fragmentContainer.setOrientation(LinearLayout.VERTICAL);


    //if (findViewById(R.id.fragment_container) != null) {

    for(int i=1;i<10;i++){
        //create a new TextFrag object
        TextFrag textFragment=new TextFrag();

        //pass arguments to the textFrag Object with the Fragment Number
        Bundle args=new Bundle();
        args.putInt("FragNumber", i);
        textFragment.setArguments(args);

        //Create a new Frame Layout for Each Fragment. This is useful if you want to switch fragments at runtime
        //Dont know exact details yet
        FrameLayout fragmentFrame=new FrameLayout(this);
        //set a unique id for the FrameLayout
        fragmentFrame.setId(i); 

        //Define the Frame Layout Params
        FrameLayout.LayoutParams params=new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);

        //CHANGED CODE STARTS HERE

        int theId=i;

        FrameLayout testFrame=(FrameLayout) findViewById(theId);

        if(testFrame!=null){

        //Add the Fragment to the Frame Layout
        getSupportFragmentManager().beginTransaction().add(testFrame.getId(),textFragment).commit();



        //Add the Frame Layout to the Linear Layout
        fragmentContainer.addView(testFrame);
        }

        //CHANGED CODE ENDS HERE

    }



}

So basically, it shows none of the framelayouts anymore because the testFrame object is now null. Any ideas on what I'm doing wrong?

Thanks to Luksprog for the answer. Simple creating an element and assigning an id to it is not enough. It needs to be added to an existing view that is referenced in the R file. In my case the existing view was R.id.fragment_container

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM