为什么我无法访问类的布局以进行测试

时间:2018-03-18 14:26:49

标签: android testing view nullpointerexception

更新:我已经扩展了代码,我的应用按预期工作,但在尝试测试activity_main.xml时仍然遇到NullPointerException。

我试图在计算器的布局中测试视图,但即使在测试文件中调用onCreate之后我仍然获得NullPointerException,我还尝试在测试中设置内容视图直接归档到activity_main但没有运气。

   public class MainActivity extends AppCompatActivity {

    ActivityMainBinding binding;
    Calculator calculator = new Calculator();


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

        void onCreateHelper() {
            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);
        }

        public void onOperatorClick(View view) {
            Button button = (Button) view;
            String value = this.getButtonText(button);

            calculator.operatorClick(value);
            String result = String.valueOf(calculator.getRunningTotal());
            binding.editText.setText(result);
        }

        public void onClearClick (View view) {
            Button button = (Button) view;
            String value = this.getButtonText(button);

            binding.editText.setText("");
            calculator.clearClick();
        }

        String getButtonText(Button button) {
            String result = button.getText().toString();
            return result;
        }

        void setValueView(String value){
            binding.editText.setText(binding.editText.getText() + value);
        }

}

测试:

    public class MainActivityTest<T extends MainActivity> {

    private MainActivity mainActivity;

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


    @Test
    public void OnNumberClickTest() throws Exception {
        mainActivity.onNumberClick(mainActivity.binding.buttonSeven);
        assertEquals(7, mainActivity.calculator.getRunningTotal());
    }

    @Test
    public void OnOperatorClickTest() throws Exception {
        mainActivity.onOperatorClick(mainActivity.binding.buttonAdd);
        assertEquals('+', mainActivity.calculator.getPreviousOperator());
    }

    @Test
    public void onClearClickTest() throws Exception {
        mainActivity.onClearClick(mainActivity.binding.buttonClear);
        assertEquals(0, mainActivity.calculator.getRunningTotal());
    }

    @Test
    public void getButtonTextTest() throws Exception {
        String buttonText = mainActivity.getButtonText(mainActivity.binding.buttonEight);
        assertEquals(8, buttonText);
    }

    @Test
    public void setValueViewTest() throws Exception {
        mainActivity.setValueView("TEST");
        assertEquals("TEST", mainActivity.binding.editText.getText());
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">



    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="99dp"
        android:orientation="vertical" android:layout_weight="0.67">



        <TextView
            android:id="@+id/editText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.50"
            android:gravity="right"
            android:paddingRight="10sp"
            android:paddingTop="5sp"
            android:textSize="15pt"
            android:textStyle="bold" />




        <TextView
            android:id="@+id/txtInput"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.50"
            android:gravity="right"
            android:paddingRight="10sp"
            android:paddingTop="5sp"
            android:textSize="15pt"
            android:textStyle="bold" />

    </LinearLayout>


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="75dp"
        android:orientation="horizontal" >


        <Button
            android:id="@+id/buttonClear"
            android:layout_width="wrap_content"
            android:layout_height="75dp"
            android:layout_weight="5"
            android:textStyle="bold"
            android:textSize="40sp"
            android:text="Clear" />

        <Button
            android:id="@+id/buttonBack"
            android:layout_width="wrap_content"
            android:layout_height="75dp"
            android:layout_weight=".51"
            android:textStyle="bold"
            android:textSize="40sp"
            android:text="BS" />

    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="75dp"
        android:orientation="horizontal" >


        <Button
            android:id="@+id/buttonSeven"
            android:layout_width="wrap_content"
            android:layout_height="75dp"
            android:layout_weight="1"
            android:textStyle="bold"
            android:textSize="40sp"
            android:text="7" />


        <Button
            android:id="@+id/buttonEight"
            android:layout_width="wrap_content"
            android:layout_height="75dp"
            android:layout_weight="1"
            android:textStyle="bold"
            android:textSize="40sp"
            android:text="8" />


        <Button
            android:id="@+id/buttonNine"
            android:layout_width="wrap_content"
            android:layout_height="75dp"
            android:layout_weight="1"
            android:textStyle="bold"
            android:textSize="40sp"
            android:text="9" />


        <Button
            android:id="@+id/buttonDivide"
            android:layout_width="wrap_content"
            android:layout_height="75dp"
            android:layout_weight="1.15"
            android:textStyle="bold"
            android:textSize="40sp"
            android:text="/" />

    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="75dp"
        android:orientation="horizontal" >


        <Button
            android:id="@+id/buttonFour"
            android:layout_width="wrap_content"
            android:layout_height="75dp"
            android:layout_weight="1"
            android:textSize="40sp"
            android:textStyle="bold"
            android:text="4" />


        <Button
            android:id="@+id/buttonFive"
            android:layout_width="wrap_content"
            android:layout_height="75dp"
            android:layout_weight="1"
            android:textStyle="bold"
            android:textSize="40sp"
            android:text="5" />


        <Button
            android:id="@+id/buttonSix"
            android:layout_width="wrap_content"
            android:layout_height="75dp"
            android:layout_weight="1"
            android:textStyle="bold"
            android:textSize="40sp"
            android:text="6" />


        <Button
            android:id="@+id/buttonMultiply"
            android:layout_width="wrap_content"
            android:layout_height="75dp"
            android:layout_weight=".98"
            android:textStyle="bold"
            android:textSize="40sp"
            android:text="x" />

    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="75dp"
        android:orientation="horizontal" >


        <Button
            android:id="@+id/buttonOne"
            android:layout_width="wrap_content"
            android:layout_height="75dp"
            android:layout_weight="1"
            android:textStyle="bold"
            android:textSize="40sp"
            android:text="1" />


        <Button
            android:id="@+id/buttonTwo"
            android:layout_width="wrap_content"
            android:layout_height="75dp"
            android:layout_weight="1"
            android:textStyle="bold"
            android:textSize="40sp"
            android:text="2" />


        <Button
            android:id="@+id/buttonThree"
            android:layout_width="wrap_content"
            android:layout_height="75dp"
            android:layout_weight="1"
            android:textStyle="bold"
            android:textSize="40sp"
            android:text="3" />


        <Button
            android:id="@+id/buttonSubtract"
            android:layout_width="wrap_content"
            android:layout_height="75dp"
            android:layout_weight="1.27"
            android:textStyle="bold"
            android:textSize="40sp"
            android:text="-" />

    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="75dp"
        android:orientation="horizontal" >


        <Button
            android:id="@+id/buttonDot"
            android:layout_width="wrap_content"
            android:layout_height="75dp"
            android:layout_weight="1.22"
            android:textStyle="bold"
            android:textSize="40sp"
            android:text="." />


        <Button
            android:id="@+id/buttonZero"
            android:layout_width="wrap_content"
            android:layout_height="75dp"
            android:layout_weight=".98"
            android:textStyle="bold"
            android:textSize="40sp"
            android:text="0" />


        <Button
            android:id="@+id/buttonEqual"
            android:layout_width="wrap_content"
            android:layout_height="75dp"
            android:layout_weight=".95"
            android:textStyle="bold"
            android:textSize="40sp"
            android:text="=" />


        <Button
            android:id="@+id/buttonAdd"
            android:layout_width="wrap_content"
            android:layout_height="75dp"
            android:layout_weight=".93"
            android:textStyle="bold"
            android:textSize="40sp"
            android:text="+" />

    </LinearLayout>

</LinearLayout>

0 个答案:

没有答案