title: This is the main activity of app. But this code somehow crashes my app. What changes should be made in order to make it work
body: <p>I'm learning how to make UI programatically in Android. I'm trying to make an app with just one button, which when clicked, a toast pops up saying how many times the button has been clicked. </p>
  
  <pre><code>package com.example.android.dynamicui;
  
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.support.v7.widget.AppCompatButton;
  import android.view.Gravity;
  import android.view.View;
  import android.widget.LinearLayout;
  import android.widget.Toast;
  
  public class MainActivity extends AppCompatActivity {
  
  private int mCount=0;
  float den = getResources().getDisplayMetrics().density;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      //Linear Layout
      LinearLayout mLayout = new LinearLayout(this);
      mLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
      mLayout.setOrientation(LinearLayout.VERTICAL);
      mLayout.setGravity(Gravity.CENTER);
  
      //Button
      AppCompatButton mButton = new AppCompatButton(this);
      mButton.setText("Click me!");
      LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
      buttonParams.setMargins(0,(int)(15*den + 0.5f),0,0);
      mButton.setLayoutParams(buttonParams);
      mButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              mCount++;
              Toast.makeText(getBaseContext(), "You pressed button for "+mCount+" times!" , Toast.LENGTH_SHORT ).show();
          }
      });
  
      //Adding button to Linear Layout
      mLayout.addView(mButton,buttonParams);
      setContentView(mLayout);
  }
  }
  </code></pre>