Ahmed Mhadhbi
Ahmed Mhadhbi

Reputation: 105

Error while trying to display something : Layout doesn't support adding with arguments

I'm getting this error whenever i try to add component to my form here's the function i'm using : this function allows me to initialize the main view for my mobile app whenver the user logs in

private void initMainView() {
        //----------------------------------------------------------------------------------------        
        User user = Controller.getThisUser();
        //----------------------------------------------------------------------------------------
        theme = UIManager.initFirstTheme("/theme");

        Form mainForm = new Form();


        final Command profileCommand = new Command("Mon Profil") {

            public void actionPerformed(ActionEvent evt) {
                mainForm.getContentPane().removeAll();
                ProfilController profilController = new ProfilController();
                profilController.initialize();
                mainForm.addComponent(BorderLayout.CENTER, profilController.getView());
                mainForm.revalidate();
            }
        };


        Image img = utilService.getImageProfilFromURL(user.getUrl());
        mainForm.addCommand(new Command("", img));
        mainForm.addCommand(profileCommand);

        Command c = new Command("Modules");
        Label l = new Label("Acceder à") {

            public void paint(Graphics g) {
                super.paint(g);
                g.drawLine(getX(), getY() + getHeight() - 1, getX() + getWidth(), getY() + getHeight() - 1);
            }
        };
        l.setUIID("Separator");
        c.putClientProperty("SideComponent", l);



        mainForm.addCommand(c);

        mainForm.addCommand(new Command("Profile", theme.getImage("all friends.png")) {


        });

        Toolbar toolBar = mainForm.getToolbar();
        toolBar.addCommandToRightBar("", theme.getImage("notifDemande.png"), (ActionListener) (ActionEvent evt) -> {
            mainForm.getContentPane().removeAll();          
            mainForm.revalidate();
        });
        toolBar.addCommandToRightBar("", theme.getImage("notifMessage.png"), (ActionListener) (ActionEvent evt) -> {
            mainForm.getContentPane().removeAll();            
            mainForm.revalidate();
        });
        toolBar.addCommandToRightBar("", theme.getImage("notifAccept.png"), (ActionListener) (ActionEvent evt) -> {
            mainForm.getContentPane().removeAll();            
            mainForm.revalidate();
        });

        mainForm.addCommand(new Command("Forum", theme.getImage("photos icon.png")) {

        });

        mainForm.addCommand(new Command("Evenement", theme.getImage("wall post.png")) {


        });

        mainForm.addCommand(new Command("Equipement", theme.getImage("wall post.png")) {


        });

        Command c1 = new Command("ACTIONS");
        Label l1 = new Label("ACTIONS") {

            public void paint(Graphics g) {
                super.paint(g);
                g.drawLine(getX(), getY() + getHeight() - 1, getX() + getWidth(), getY() + getHeight() - 1);
            }
        };
        l1.setUIID("Separator");
        c1.putClientProperty("SideComponent", l1);
        mainForm.addCommand(c1);

        mainForm.addCommand(new Command("Parametres de profil", theme.getImage("wall post.png")) {


        });

        mainForm.addCommand(new Command("Logout") {

            public void actionPerformed(ActionEvent evt) {

            }
        });

        mainForm.addCommand(new Command("Quitter") {

            public void actionPerformed(ActionEvent evt) {
                Display.getInstance().exitApplication();
            }
        });



        mainForm.show();
    }

and this is my initialize function in the profilController controller :

@Override
    public void initialize() {
        Container c = new Container(new BoxLayout(BoxLayout.Y_AXIS));
        c.setScrollableY(true);


        User u = this.getThisUser();

        Container changec = new Container(new BoxLayout(BoxLayout.Y_AXIS));

        Container entete = new Container(new BoxLayout(BoxLayout.X_AXIS));
        //c.add(theme.getImage(t.getImg()));
        Image img = utilService.getImageProfilFromURL(u.getUrl());
        Label nt = new Label(u.getNom()+" "+u.getPrenom());
        entete.add(img).add(nt);
        //c.add(entete);        
        Button journal = new Button("Journal");


        Button apropos = new Button("A Propos");

        Button album = new Button("Album");

        c.add(entete);
        c.add(journal);
        c.add(apropos);
        c.add(album);
        c.add(changec);
        //------------
        this.rootContainer.removeAll();
        this.rootContainer.add(BorderLayout.NORTH, c);
        this.rootContainer.revalidate();
    }

i'm new to codenameone and i don't know what to do to solve this , any ideas ?

EDIT : here's the console output :

java.lang.IllegalStateException: Layout doesn't support adding with arguments: com.codename1.ui.layouts.FlowLayout
    at com.codename1.ui.layouts.Layout.addLayoutComponent(Layout.java:66)
    at com.codename1.ui.Container.addComponent(Container.java:746)
    at com.codename1.ui.Form.addComponent(Form.java:1548)
    at com.mycompany.myapp.MyApplication$2.actionPerformed(MyApplication.java:182)
    at com.codename1.ui.SideMenuBar$CommandWrapper.actionPerformed(SideMenuBar.java:1809)
    at com.codename1.ui.util.EventDispatcher.fireActionEvent(EventDispatcher.java:349)
    at com.codename1.ui.Button.fireActionEvent(Button.java:563)
    at com.codename1.ui.Button.released(Button.java:605)
    at com.codename1.ui.Button.pointerReleased(Button.java:709)
    at com.codename1.ui.Form.pointerReleased(Form.java:3397)
    at com.codename1.ui.SideMenuBar$8.pointerReleased(SideMenuBar.java:1226)
    at com.codename1.ui.Component.pointerReleased(Component.java:4555)
    at com.codename1.ui.Display.handleEvent(Display.java:2193)
    at com.codename1.ui.Display.edtLoopImpl(Display.java:1156)
    at com.codename1.ui.Display.mainEDTLoop(Display.java:1074)
    at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
    at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)

Upvotes: 1

Views: 359

Answers (2)

Lequi
Lequi

Reputation: 549

In addition to Shai's answer, I suggest you lookup the layout basics of Codename One, as this will be essential in your future development.

Written documentation here: https://www.codenameone.com/manual/basics.html

if you prefer watching videos, this is for you: https://www.codenameone.com/how-do-i---positioning-components-using-layout-managers.html

Upvotes: 1

Shai Almog
Shai Almog

Reputation: 52770

You are invoking this:

mainForm.addComponent(BorderLayout.CENTER, profilController.getView());

But you created your form like this:

Form mainForm = new Form();

You probably should have created the Form like this as your default layout would be FlowLayout which rarely makes sense:

Form mainForm = new Form(new BorderLayout());

The exception was thrown because with a flow layout this is the legal syntax:

mainForm.addComponent(profilController.getView());

Upvotes: 2

Related Questions