1

hey guys need a little help........my intention is to develop an app which contains just a button... and on clicking it it must create another button on the screen dynamically.......

here's my code...

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Button bee=new Button(getBaseContext());
            bee.setText("hello:");
             RelativeLayout a;
            a=(RelativeLayout)findViewById(R.layout.activity_main);
    a.addView(bee,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        }
    });

this code is error free but when executed says "unfortunately 'myapplication' has stopped"....

pls do help guys..( my sole aim is to create widgets dynamically ,hence tried dis program). if u have any other suggestions pls post nd comment

3
  • 3
    Welcome to SO. Please look at the stacktrace in the logcat and post it in your question. Commented Nov 2, 2012 at 9:49
  • 1
    Check logcat for the reason for your application crashing. Commented Nov 2, 2012 at 9:49
  • 1
    Welcome to stackoverflow.. Please note these things 1)When you post a question, please make sure its title is valid. android developer (beginer) is not one. 2) If you get crash, most important thing you need to post is crash log Commented Nov 2, 2012 at 9:49

5 Answers 5

1
 a=(RelativeLayout)findViewById(R.layout.activity_main);

activity_main is a layout, not a RelativeLayout widget!

you should use

 a=(RelativeLayout)findViewById(R.id.my_relative_layout);

Please post the stack trace (logcat) so you will get better and faster answer.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this.

RelativeLayout rLayout = (RelativeLayout )findViewById(R.id.relativeLayout); // Here this should be id of your relative layout.
Button btn = new Button(this);
rLayout.addView(btn);

Comments

0

try with this code i think you are getting problem just because of not setting the parameters properly so check with this code-

 LinearLayout container = (LinearLayout)findViewById(R.id.container);

    Button btn = new Button(this);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.MATCH_PARENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT);
    btn.setLayoutParams(lp);

    container.addView(btn);

Comments

0

You are finding id rather then layout

so change this line:

a=(RelativeLayout)findViewById(R.id.activity_main); 

Comments

0

You are trying to "find" a layout which can't exist! Please, make sure you have defined a RealtiveLayout with a valid id and then do

a=(RelativeLayout)findViewById(R.id.valid_layout_id);

instead of

a=(RelativeLayout)findViewById(R.layout.activity_main);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.