title: Passing a checkbox state from XML to Java
body: <p>I am new to android development, I am trying to pass a checkbox state using findViewById method, but the app gets crashed:</p>
  
  <p>Here is the xml code:</p>
  
  <pre><code>&lt;CheckBox
          android:id="@+id/toppings_checkbox"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginLeft="16sp"
          android:layout_marginTop="8sp"
          android:layout_marginBottom="16sp"
          android:text="Whipped Cream"
          android:textColor="#FFFFFF"
          android:textSize="20sp"
          android:paddingLeft="16dp"
          android:buttonTint="#FFFFFF"
          /&gt;
  </code></pre>
  
  <p>and here is the java statement to link this checkbox with java:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  CheckBox hasWhippedCream = (CheckBox) findViewById(R.id.toppings_checkbox);
  boolean checked = hasWhippedCream.isChecked();
  int quantity = 0;
  int pricePerCup = 5;
  int toppingPrice = 0;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  }
  
  /**
   * This method is called when the + button is clicked
   */
  public void Increment(View view) {
      quantity = quantity + 1;
      displayQuantity(quantity);
  }
  
  /**
   * This method is called when the - button is clicked
   */
  public void Decrement(View view) {
      quantity = quantity - 1;
      if (quantity &lt; 1) {
          quantity = 1;
      }
      displayQuantity(quantity);
  }
  
  /**
   * This method is called when the order button is clicked.
   */
  public void submitOrder(View view) {
      displayMessage(createOrderSummary(calculatePrice()));
  }
  
  /**
   * Calculates the price of the order.
   */
  private int calculatePrice() {
  
      if (checked = true) {
          toppingPrice = 2;
      } else {
          toppingPrice = 0;
      }
      return quantity * (pricePerCup + toppingPrice);
  }
  
  /**
   * This method displays summary of the given order.
   *
   * @param total is the total price of the order.
   *              * @return text summary
   */
  
  private String createOrderSummary(int total) {
  
      String summary = "Name: Captain Samir\n";
      summary += "Add whipped cream? " + checked + "\n";
      summary += "Quantity: " + quantity + " Cups\n";
      summary += "Total: $" + total + "\n";
      summary += "Thank You!";
      return summary;
  }
  
  /**
   * This method displays the given quantity value on the screen.
   */
  private void displayQuantity(int value) {
      TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
      quantityTextView.setText("" + value);
  }
  
  /**
   * This method displays the given text on the screen.
   */
  private void displayMessage(String message) {
      TextView orderSummaryTextView = (TextView) findViewById(R.id.order_summary_text_view);
      orderSummaryTextView.setText(message);
  }
  }
  </code></pre>
  
  <p>and then I use the variable <code>checked</code> through the program. any idea why the app crashes once starts up?</p>