简体   繁体   中英

Android Studio Cannot resolve symbol

I am a beginner trying to lean android development, so I am taking google's android for beginner course, i have followed all the instructions but I get the error "Cannot resolve symbol price" in : displayMessage(createOrderSummary(price));

This is part of the code ...

/**
 * This method is called when the order button is clicked.
 */
public void submitOrder(View view) {
    int Price = calculatePrice();
    displayMessage(createOrderSummary(*price*));
        }


/**
 * Calculates the price of the order.
 *
 * @return total price
 */

public int calculatePrice() {
    return quantity * 5;
       }

/**
 *this method will create the order summary.
 *@param price of the order
 *@return text summary
*/

private String createOrderSummary (int price){
    String priceMessage = "Name: Ana";
    priceMessage +=  "\n Price;Quantity:" + quantity;
    priceMessage +=  "\nTotal $"+ price;
    priceMessage +=  "\n Thank You!";
    return priceMessage;
}

Replace:

public void submitOrder(View view) {
    int Price = calculatePrice();
    displayMessage(createOrderSummary(*price*));
}

With:

public void submitOrder(View view) {
    int price = calculatePrice();
    displayMessage(createOrderSummary(price));
}

The crux of the issue appears to be that price shouldn't be placed between asterisks.

Also, whilst it isn't essential to swap int Price for int price , it is common convention in Android to make variable names (like price ) lower case for their first character, and reserve upper-case first characters (ie Price ) for class names.

You are referencing a variable which does not exist. Variable names should start with a lowercase letter, so instead of Price , use price :

public void submitOrder(View view) {
    int price = calculatePrice();
    displayMessage(createOrderSummary(price));
}

Also note that Java is case-sensitive, so if you name something price , you can't refer to it by Price . The names of classes (types) should start with a capital letter though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM