I want to take input from user in edit text field in android studio by manual typing by the user and clicking buttons as well. But some sorted out method on this site regarding this challenge is not working for me and throwing an exception (Attempt to invoke virtual method 'android.view.Window$Callbackandroid.view.Window.getCallback()' on a null object reference)
public class Main2Activity extends AppCompatActivity {
int quantity = getQuantity();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
// Increment button
public void increment(View view){
quantity = quantity + 1;
while (quantity > 10){
quantity = quantity - 1;
}
setQuantity(quantity);
}
// Decrement button
public void decrement(View view){
quantity = quantity - 1;
while (quantity < 1){
quantity = quantity + 1;
}
setQuantity(quantity);
}
private int getQuantity(){
EditText editText = findViewById(R.id.qty_edit_text);
int quantity = Integer.parseInt(editText.getText().toString());
return quantity;
}
private void setQuantity(int number){
EditText editText = findViewById(R.id.qty_edit_text);
editText.setText("" + number);
}
}