dimanche 18 mars 2018

Why can't I access the layout of a class for testing in Android studio

I'm trying to test the views in a layout for a calculator but I keep getting nullPointerException even after calling onCreate in the test file, I've also tried setting content view inside the test file directly to the activity_main but no luck.

public class MainActivity extends AppCompatActivity {

    public ActivityMainBinding binding;
    Calculator calculator = new Calculator();


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            onCreateHelper();
        }

        void onCreateHelper() {
            this.setContentView(R.layout.activity_main);
        }

Test:

public class MainActivityTest {

    private MainActivity mainActivity;

    @Before
    public void before() {
        mainActivity = new MainActivity();
        mainActivity.onCreateHelper();
    }

I've also tried:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
}

public void onNumberClick(View view) {
    Button button = (Button) view;
    String result = this.getButtonText(button);
    this.setValueView(result);

    int number = Integer.parseInt(result);
    calculator.numberClick(number);
}

Aucun commentaire:

Enregistrer un commentaire