0

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);
    }

}
Android
  • 1,420
  • 4
  • 13
  • 23
Arnab
  • 1
  • 1
  • 1
    Possible duplicate of [Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference](https://stackoverflow.com/questions/36666091/attempt-to-invoke-virtual-method-android-view-windowcallback-android-view-wind) – Bö macht Blau Oct 14 '18 at 18:57

3 Answers3

1

You are calling getQuantity() function before setContentView(int res) is called. First of all don't initialize your EditText in getQuantity() and setQuantity(int) Create EditText editText variable in Main2Activity class, but outside onCreate(Bundle) method. Just like you create int quantity. It should looks like this:

int quantity = 0;
EditText editText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    editText = findViewById(R.id.qty_edit_text);
    quantity = getQuantity();
}

This way you can acces your editText from every method in Main2Activity class

Da Artagnan
  • 1,109
  • 3
  • 17
  • 26
  • Your method saved the app from crashing but manually typed value isn't getting assigned to quantity variable. Only button pressed value is being assigned. – Arnab Oct 15 '18 at 06:31
0

Why don't you use button.setOnClickListener()instead of public void increment or decrement.make edit text global and on onCreate() method, set resource id for edit text.

Sohail Shrestha
  • 477
  • 5
  • 6
0

Here is the elaborated version. LOL

public class Main2Activity extends AppCompatActivity {
int quantity;
EditText editText;
Button IncrementButton, DecrementButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    quantity = getQuantity();
    editText = findViewById(R.id.qty_edit_text);
    IncrementButton = //findViewByid....
    DecrementButton = //FindViewByID

   IncrementButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           quantity = quantity + 1;
    while (quantity > 10){
        quantity = quantity - 1;
    }
    setQuantity(quantity);
        }
    });
  DecrementButton.etOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            quantity = quantity - 1;
           while (quantity < 1){
          quantity = quantity + 1;
    }
        setQuantity(quantity);    
          }

 }




private int getQuantity(){
    int quantity = Integer.parseInt(editText.getText().toString());
    return quantity;
}

private void setQuantity(int number){
    editText.setText("" + number);
}

}
Sohail Shrestha
  • 477
  • 5
  • 6