NullPointerException in Activity Testing Tutorial

I am currently trying to follow the activity testing tutorial ( Found here ) and are having problems. It seems that whenever I try to call something inside UIThread, I get a java.lang.NullPointerException exception.

public void testSpinnerUI() {
    mActivity.runOnUiThread( new Runnable() {  
        public void run() {  
            mSpinner.requestFocus();
        }  
    });  
}

This gives me:

Incomplete: java.lang.NullPointerException

and nothing else. I tried this on two different samples now, with the same result. I tried with a try / catch clause around calling mSpinner.requestFocus (), and it seems that mSpinner has zero inside the stream. I set the setUp () function found in the same sample correctly, and the quick assertNotNull (mSpinner) shows me that mSpinner is actually not null after the setUp () function. What could be the reason for this?

EDIT; ok, a few more tests have been done. It seems that the test application is reset between each test. This essentially makes me reset all the variables between each test. This is normal?

+3
source share
6 answers

, , . , . runOnUiThread(), , @UiThreadTest . NullPointerExceptions, setUp() . , , , , :)

+6

. , runOnUiThread(), NPE . , , .

, runOnUiThread , . , , , . , , UIEvent .

, CyclicBarrier, , , UiThread. encapuslate Activity.runOnUiThread() :

  private void myRunOnUiThread( final Runnable r) {
    final CyclicBarrier barrier = new CyclicBarrier(2);
    mActivity.runOnUiThread(
      new Runnable() {
        public void run() {
          r.run();
          try { barrier.await(); } catch (Exception e) { e.printStackTrace(); }
    }});
    try { barrier.await(); } catch (Exception e) { e.printStackTrace(); }
  }

:

public void testFieldUI() {
        myRunOnUiThread(
                new Runnable() {
                    public void run() {
                        mRateView.requestFocus();
                        mRateView.selectAll();
                    } 
                } 
        ); 
        sendKeys(KeyEvent.KEYCODE_DEL); // delete everything
        assertTrue("rate is deleted", mRateView.getText().length() == 0);
+3

, , sendKey() , :

public void testSoundSpinnerUI() {
    myActivity.runOnUiThread(new Runnable() {
        public void run() {
            mySoundSpinner.requestFocus();
            mySoundSpinner.setSelection(0);
        }
    });

    sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
    sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
    sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
}
+1

, . , , , - .

, . , , .

...

+1

, , , , Test class - UI-. Null Pointer UI-thread.

, boolean running, , UI- . UI- , "main'-thread", , running. , , , 200 , . UI- , runnable false .

public void testSomething() {
    activity.runOnUiThread(
            new Runnable() {
                public void run() {
                    // Run something on UI-thread

                    // In the end of this thread, set stillRunning false
                    stillRunning = false;
                }
            });

    while(stillRunning) { 
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            // Handle exception
        } 
    }
}
+1

, . - , NullPointException. , , getActivity() .

0
source

Source: https://habr.com/ru/post/1750135/


All Articles