Android - Display Int onClick with Buttons increment , decremet [Error: Cannot resolve method ‘display(int)’ ] I am getting a error on Android Studio as what I believe is because I'm inserting multiple 'displays'. Can someone advise & explain what I need to do and why it causes this error so I know for future?
Please see below for the following error:
package com.example.justjava;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.text.NumberFormat;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView num = findViewById(R.id.num);
Button order1 = findViewById(R.id.order);
/**
* This method is called when the plus button is clicked.
*/
public void increment (View view) {
int quantity = 2;
display(quantity);
}
/**
* This method is called when the minus button is clicked.
*/
public void onClick (View view) {
int quantity = 2;
display(quantity);
}
/**
* This method is called when the order button is clicked.
*/
order1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int quantity = 2;
display1(quantity);
displayPrice(quantity*5);
}
/**
* This method displays the given price on the screen.
*/
private void displayPrice(int number) {
TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
}
/**
* This method displays the given quantity value on the screen.
*/
private void display1(int number)
{
// TextView quantity = (TextView) findViewById(R.id.quantity);
num.setText("" number);
}
});
}
}
CodePudding user response:
In Java, we don't have anything called Local Function as a result, move the functions from the onCreate method to the class.
CodePudding user response:
Firstly, implement your methods onClick() and increment() outside of the function onCreate(). It does not make sense to implement a function inside of a fuction. You can implement them inside the class MainActivity. Moreover, I would suggest not to name your custom button click function as onClick() as it might lead to confusion later on.
Secondly, I cannot see the implementation of the function "display()". That's why AndroidStudio cannot recognize the function. If you move onClick() and increment() to the place I said and in the same class implement the display function, it should be fine.