有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java空指针异常Robotium单元测试

我正在尝试使用机器人进行单元测试。。。每次我尝试时都会出现空指针异常。。。一切正常,直到应用加载,然后出现异常。。。在本教程中,据说会发生此错误,但解决方案尚不清楚。。请帮忙。。代码如下:

package com.testCalculator;
import 安卓.test.ActivityInstrumentationTestCase2;  
import com.jayway.安卓.robotium.solo.Solo;

@SuppressWarnings("unchecked")
public class TestApk extends ActivityInstrumentationTestCase2 {
    private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.calculator.Main";

    private static Class launcherActivityClass;
    static {
        try {
            launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
        } 
        catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public TestApk() throws ClassNotFoundException {
        super(launcherActivityClass);
    }

    private Solo solo;
    @Override
    protected void setUp() throws Exception {
        solo = new Solo(getInstrumentation(), getActivity());
    }

    public void testDisplayBlackBox() {
        // Enter any integer/decimal value for first edit-field, we are writing 10
        solo.clearEditText(0);
        solo.enterText(0, "10");
        // Enter any integer/decimal value for first edit-field, we are writing 20
        solo.clearEditText(1);
        solo.enterText(1, "20");
        // Tap on Multiply button
        solo.clickOnButton("Multiply");
        //Assert that the correct resultant of 10 x 20 is returned
        assertTrue("Problem asserting multiply", solo.searchText("200"));
    }

    @Override
    public void tearDown() throws Exception {
        solo.finishOpenedActivities();
    }
}

黑盒测试失败跟踪:

TestApk (2)
samsung-samsung_sm_n900a-a0964e35
com.testCalculator.TestApk
testDisplayBlackBox (com.testCalculator.TestApk) [samsung-samsung_sm_n900a-a0964e35]
java.lang.NullPointerException

at com.jayway.安卓.robotium.solo.ViewFetcher.getNonDecorViews(ViewFetcher.java:163)

at com.jayway.安卓.robotium.solo.ViewFetcher.getAllViews(ViewFetcher.java:85)

at com.jayway.安卓.robotium.solo.ViewFetcher.getViews(ViewFetcher.java:194)

at com.jayway.安卓.robotium.solo.ViewFetcher.getCurrentViews(ViewFetcher.java:312)

at com.jayway.安卓.robotium.solo.ViewFetcher.getCurrentViews(ViewFetcher.java:299)

at com.jayway.安卓.robotium.solo.Searcher.searchFor(Searcher.java:123)

at com.jayway.安卓.robotium.solo.Waiter.waitForView(Waiter.java:105)

at com.jayway.安卓.robotium.solo.Waiter.waitForAndGetView(Waiter.java:348)

at com.jayway.安卓.robotium.solo.Solo.enterText(Solo.java:1290)

at com.testCalculator.TestApk.testDisplayBlackBox(TestApk.java:29)

at java.lang.reflect.Method.invokeNative(Native Method)

at 安卓.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)

at 安卓.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)

at 安卓.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)

at 安卓.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)

at 安卓.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)

at 安卓.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)

at 安卓.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1741)

记录cat输出:

07-18 10:36:28.084: I/dalvikvm(31894): Enabling JNI app bug workarounds for target SDK version 8...
07-18 10:36:28.134: I/TestRunner(31894): started: testDisplayBlackBox(com.testCalculator.TestApk)
07-18 10:36:28.154: I/TestRunner(31894): finished: testDisplayBlackBox(com.testCalculator.TestApk)
07-18 10:36:28.154: I/TestRunner(31894): passed: testDisplayBlackBox(com.testCalculator.TestApk)
07-18 10:36:28.924: I/dalvikvm(31977): Enabling JNI app bug workarounds for target SDK version 8...
07-18 10:36:28.964: I/TestRunner(31977): started: testDisplayBlackBox(com.testCalculator.TestApk)
07-18 10:36:29.014: W/ApplicationPackageManager(31977): getCSCPackageItemText()
07-18 10:36:29.124: I/Adreno-EGL(31977): <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build:  ()
07-18 10:36:29.124: I/Adreno-EGL(31977): OpenGL ES Shader Compiler Version: E031.24.00.07
07-18 10:36:29.124: I/Adreno-EGL(31977): Build Date: 01/22/14 Wed
07-18 10:36:29.124: I/Adreno-EGL(31977): Local Branch: base_au149_adreno_au169_patches
07-18 10:36:29.124: I/Adreno-EGL(31977): Remote Branch: 
07-18 10:36:29.124: I/Adreno-EGL(31977): Local Patches: 
07-18 10:36:29.124: I/Adreno-EGL(31977): Reconstruct Branch: 
07-18 10:36:29.164: D/OpenGLRenderer(31977): Enabling debug mode 0
07-18 10:36:29.254: V/RenderScript(31977): Application requested CPU execution
07-18 10:36:29.254: V/RenderScript(31977): 0x78878260 Launching thread(s), CPUs 4
07-18 10:36:29.274: E/dalvikvm(31977): Could not find class '安卓.support.v4.app.FragmentActivity', referenced from method com.jayway.安卓.robotium.solo.Waiter.getSupportFragment
07-18 10:36:29.274: W/dalvikvm(31977): VFY: unable to resolve check-cast 21 (L安卓/support/v4/app/FragmentActivity;) in Lcom/jayway/安卓/robotium/solo/Waiter;
07-18 10:36:29.274: D/dalvikvm(31977): VFY: replacing opcode 0x1f at 0x0009
07-18 10:36:29.274: I/dalvikvm(31977): Could not find method 安卓.support.v4.app.FragmentActivity.getSupportFragmentManager, referenced from method com.jayway.安卓.robotium.solo.Waiter.getSupportFragment
07-18 10:36:29.274: W/dalvikvm(31977): VFY: unable to resolve virtual method 26: L安卓/support/v4/app/FragmentActivity;.getSupportFragmentManager ()L安卓/support/v4/app/FragmentManager;
07-18 10:36:29.274: D/dalvikvm(31977): VFY: replacing opcode 0x6e at 0x0010
07-18 10:36:29.274: I/dalvikvm(31977): Could not find method 安卓.support.v4.app.FragmentActivity.getSupportFragmentManager, referenced from method com.jayway.安卓.robotium.solo.Waiter.getSupportFragment
07-18 10:36:29.274: W/dalvikvm(31977): VFY: unable to resolve virtual method 26: L安卓/support/v4/app/FragmentActivity;.getSupportFragmentManager ()L安卓/support/v4/app/FragmentManager;
07-18 10:36:29.274: D/dalvikvm(31977): VFY: replacing opcode 0x6e at 0x0019
07-18 10:36:29.274: W/dalvikvm(31977): VFY: unable to find class referenced in signature (L安卓/support/v4/app/Fragment;)
07-18 10:36:29.274: W/dalvikvm(31977): VFY: unable to find class referenced in signature (L安卓/support/v4/app/Fragment;)
07-18 10:36:29.774: W/System.err(31977): java.lang.NoSuchFieldException: mViews
07-18 10:36:29.784: W/System.err(31977):    at java.lang.Class.getDeclaredField(Class.java:596)
07-18 10:36:29.784: W/System.err(31977):    at com.jayway.安卓.robotium.solo.ViewFetcher.getWindowDecorViews(ViewFetcher.java:382)
07-18 10:36:29.784: W/System.err(31977):    at com.jayway.安卓.robotium.solo.ViewFetcher.getAllViews(ViewFetcher.java:83)
07-18 10:36:29.784: W/System.err(31977):    at com.jayway.安卓.robotium.solo.ViewFetcher.getViews(ViewFetcher.java:194)
07-18 10:36:29.784: W/System.err(31977):    at com.jayway.安卓.robotium.solo.ViewFetcher.getCurrentViews(ViewFetcher.java:312)
07-18 10:36:29.784: W/System.err(31977):    at com.jayway.安卓.robotium.solo.ViewFetcher.getCurrentViews(ViewFetcher.java:299)
07-18 10:36:29.784: W/System.err(31977):    at com.jayway.安卓.robotium.solo.Searcher.searchFor(Searcher.java:123)
07-18 10:36:29.784: W/System.err(31977):    at com.jayway.安卓.robotium.solo.Waiter.waitForView(Waiter.java:105)
07-18 10:36:29.784: W/System.err(31977):    at com.jayway.安卓.robotium.solo.Waiter.waitForAndGetView(Waiter.java:348)
07-18 10:36:29.784: W/System.err(31977):    at com.jayway.安卓.robotium.solo.Solo.clearEditText(Solo.java:1339)
07-18 10:36:29.784: W/System.err(31977):    at com.testCalculator.TestApk.testDisplayBlackBox(TestApk.java:27)
07-18 10:36:29.784: W/System.err(31977):    at java.lang.reflect.Method.invokeNative(Native Method)
07-18 10:36:29.794: W/System.err(31977):    at java.lang.reflect.Method.invoke(Method.java:515)
07-18 10:36:29.794: W/System.err(31977):    at 安卓.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
07-18 10:36:29.794: W/System.err(31977):    at 安卓.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
07-18 10:36:29.794: W/System.err(31977):    at 安卓.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
07-18 10:36:29.794: W/System.err(31977):    at junit.framework.TestCase.runBare(TestCase.java:134)
07-18 10:36:29.794: W/System.err(31977):    at junit.framework.TestResult$1.protect(TestResult.java:115)
07-18 10:36:29.794: W/System.err(31977):    at junit.framework.TestResult.runProtected(TestResult.java:133)
07-18 10:36:29.794: W/System.err(31977):    at junit.framework.TestResult.run(TestResult.java:118)
07-18 10:36:29.794: W/System.err(31977):    at junit.framework.TestCase.run(TestCase.java:124)
07-18 10:36:29.794: W/System.err(31977):    at 安卓.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
07-18 10:36:29.794: W/System.err(31977):    at 安卓.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
07-18 10:36:29.794: W/System.err(31977):    at 安卓.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
07-18 10:36:29.794: W/System.err(31977):    at 安卓.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1741)
07-18 10:36:29.994: W/IInputConnectionWrapper(31977): showStatusIcon on inactive InputConnection
07-18 10:36:30.734: I/TestRunner(31977): failed: testDisplayBlackBox(com.testCalculator.TestApk)
07-18 10:36:30.734: I/TestRunner(31977): ----- begin exception -----
07-18 10:36:30.734: I/TestRunner(31977): java.lang.NullPointerException
07-18 10:36:30.734: I/TestRunner(31977):    at com.jayway.安卓.robotium.solo.ViewFetcher.getNonDecorViews(ViewFetcher.java:163)
07-18 10:36:30.734: I/TestRunner(31977):    at com.jayway.安卓.robotium.solo.ViewFetcher.getAllViews(ViewFetcher.java:85)
07-18 10:36:30.734: I/TestRunner(31977):    at com.jayway.安卓.robotium.solo.ViewFetcher.getViews(ViewFetcher.java:194)
07-18 10:36:30.734: I/TestRunner(31977):    at com.jayway.安卓.robotium.solo.ViewFetcher.getCurrentViews(ViewFetcher.java:312)
07-18 10:36:30.734: I/TestRunner(31977):    at com.jayway.安卓.robotium.solo.ViewFetcher.getCurrentViews(ViewFetcher.java:299)
07-18 10:36:30.734: I/TestRunner(31977):    at com.jayway.安卓.robotium.solo.Searcher.searchFor(Searcher.java:123)
07-18 10:36:30.734: I/TestRunner(31977):    at com.jayway.安卓.robotium.solo.Waiter.waitForView(Waiter.java:105)
07-18 10:36:30.734: I/TestRunner(31977):    at com.jayway.安卓.robotium.solo.Waiter.waitForAndGetView(Waiter.java:348)
07-18 10:36:30.734: I/TestRunner(31977):    at com.jayway.安卓.robotium.solo.Solo.clearEditText(Solo.java:1339)
07-18 10:36:30.734: I/TestRunner(31977):    at com.testCalculator.TestApk.testDisplayBlackBox(TestApk.java:27)
07-18 10:36:30.734: I/TestRunner(31977):    at java.lang.reflect.Method.invokeNative(Native Method)
07-18 10:36:30.734: I/TestRunner(31977):    at java.lang.reflect.Method.invoke(Method.java:515)
07-18 10:36:30.734: I/TestRunner(31977):    at 安卓.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
07-18 10:36:30.734: I/TestRunner(31977):    at 安卓.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
07-18 10:36:30.734: I/TestRunner(31977):    at 安卓.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
07-18 10:36:30.734: I/TestRunner(31977):    at junit.framework.TestCase.runBare(TestCase.java:134)
07-18 10:36:30.734: I/TestRunner(31977):    at junit.framework.TestResult$1.protect(TestResult.java:115)
07-18 10:36:30.734: I/TestRunner(31977):    at junit.framework.TestResult.runProtected(TestResult.java:133)
07-18 10:36:30.734: I/TestRunner(31977):    at junit.framework.TestResult.run(TestResult.java:118)
07-18 10:36:30.734: I/TestRunner(31977):    at junit.framework.TestCase.run(TestCase.java:124)
07-18 10:36:30.734: I/TestRunner(31977):    at 安卓.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
07-18 10:36:30.734: I/TestRunner(31977):    at 安卓.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
07-18 10:36:30.734: I/TestRunner(31977):    at 安卓.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
07-18 10:36:30.734: I/TestRunner(31977):    at 安卓.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1741)
07-18 10:36:30.734: I/TestRunner(31977): ----- end exception -----
07-18 10:36:30.734: I/TestRunner(31977): finished: testDisplayBlackBox(com.testCalculator.TestApk)

控制台输出:

Android Launch!
[2014-07-18 10:36:09 - TestCalculator] adb is running normally.
[2014-07-18 10:36:09 - TestCalculator] Performing 安卓.test.InstrumentationTestRunner JUnit launch
[2014-07-18 10:36:09 - TestCalculator] Automatic Target Mode: Unable to detect device compatibility. Please select a target device.
[2014-07-18 10:36:11 - TestCalculator] Uploading TestCalculator.apk onto device 'a0964e35'
[2014-07-18 10:36:11 - TestCalculator] Installing TestCalculator.apk...
[2014-07-18 10:36:16 - TestCalculator] Success!
[2014-07-18 10:36:16 - TestCalculator] Project dependency found, installing: AndroidCalculator
[2014-07-18 10:36:16 - AndroidCalculator] Uploading AndroidCalculator.apk onto device 'a0964e35'
[2014-07-18 10:36:16 - AndroidCalculator] Installing AndroidCalculator.apk...
[2014-07-18 10:36:27 - AndroidCalculator] Success!
[2014-07-18 10:36:27 - TestCalculator] Launching instrumentation 安卓.test.InstrumentationTestRunner on a0964e35
[2014-07-18 10:36:28 - TestCalculator] Sending test information to Eclipse
[2014-07-18 10:36:31 - TestCalculator] Test run finished

共 (2) 个答案

  1. # 2 楼答案

    嗯。。这确实奏效了。。。 解决方案:将Robotium更新至最新版本。。目前是5.1