Baffo rasta Baffo rasta - 1 year ago 101
Android Question

Why does this Android application immediately crash?

I leave you the code:

public void onCreate(Bundle savedInstanceState)
RelativeLayout layout = (RelativeLayout)findViewById(;
Button pulsante = new Button(this);
pulsante.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
pulsante.setText("Click me");
pulsante.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Thug life bitch!", Toast.LENGTH_LONG).show();


I tried to remove all the lines from the RelativeLayout to the layout.addView one and so it works again!

What's wrong in this code? I must say that I don't get any build error or warnings.

Thanks in advance.

Answer Source

write setContentView() immediate after super.onCreate()


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download