Android ActivityInstrumentationTestCase2 NullPointerException on getting context

亡梦爱人 提交于 2019-12-24 07:02:14

问题


I have an ActivityInstrumentationTestCase2 and I have had no luck getting context for the test.

package com.vsnetworks.vsnmedia.test;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.test.ActivityInstrumentationTestCase2;

import com.vsnetworks.vsnmedia.MainActivity;
import com.vsnetworks.vsnmedia.VSWebViewClient;

public class TestMimeTypes extends ActivityInstrumentationTestCase2<MainActivity> {

Activity activity;
Context context;

public TestMimeTypes() {
    super(MainActivity.class);
}

public void setUp() throws Exception {
    super.setUp();
    activity = getActivity();
    context = activity.getApplicationContext();
}

public void test() {
    String external = context.getExternalFilesDir(null).toString();
}

Here's the error I get:

 [exec] com.vsnetworks.vsnmedia.test.TestMimeTypes:INSTRUMENTATION_RESULT: shortMsg=java.lang.NullPointerException
 [exec] INSTRUMENTATION_RESULT: longMsg=java.lang.NullPointerException: Unable to start activity ComponentInfo{com.vsnetworks.vsnmedia/com.vsnetworks.vsnmedia.MainActivity}: java.lang.NullPointerException
 [exec] INSTRUMENTATION_CODE: 0

I think it's context related because if I do the below:

Context context = this.getInstrumentation().getTargetContext();

I get (line 37 is context.getExternal line)

 [exec] com.vsnetworks.vsnmedia.test.TestMimeTypes:
 [exec] Error in test:
 [exec] java.lang.NullPointerException
 [exec]     at com.vsnetworks.vsnmedia.test.TestMimeTypes.test(TestMimeTypes.java:37)
 [exec]     at java.lang.reflect.Method.invokeNative(Native Method)
 [exec]     at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
 [exec]     at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
 [exec]     at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
 [exec]     at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
 [exec]     at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
 [exec]     at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
 [exec]     at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)
 [exec] 
 [exec] Test results for InstrumentationTestRunner=..E
 [exec] Time: 0.292
 [exec] 
 [exec] FAILURES!!!
 [exec] Tests run: 2,  Failures: 0,  Errors: 1

I have also tried all the below with the same errors, even with getInstrumentation.waitForIdleSync() (which I saw in another thread) in a few places as well. I've tried creating new MainActivity() objects and getting context from there as well as getActivity(), same problem.

context = getInstrumentation().getContext();
context = getInstrumentation().getTargetContext();
context = activity.getApplicationContext();
context = activity.getBaseContext();

So what in the world am I doing wrong?

Edit 1 - It would appear this problem only occurs on an emulator. If I use a real device to run the tests they both pass.


回答1:


I think this is what you're looking for:

public void testExternalFilesDir() {
    Activity activity = getActivity();
    assertNotNull(activity);
    File file = activity.getExternalFilesDir(null);
    assertNotNull("No external storage available", file);
}



回答2:


There was actually two problems I had to solve. The first was that I had an external library included in my main project. When running the tests, the external library was not being properly added. When creating main, it was failing silently upon trying to use the library. To solve that I followed the various solutions here:

Can't build and run an android test project created using "ant create test-project" when tested project has jars in libs directory

Which led me to this one:

Android unit test using ant with library project

Android unit test using ant with library project R18

My fix differed slightly, however. Instead of using the overridden -compile tag in those projects, I simply copied the build.xml compile in my /tools/ant into my test's build.xml and added

            <classpath>
                <!-- steff: we changed one line here !-->
                <fileset dir="${tested.android.library.reference.1}/bin/" includes="*.jar"/>
                <fileset dir="${extensible.libs.classpath}" includes="*.jar" />
            </classpath>

In the same spot as the second answer's.

The second problem was that the sd card the emulator was trying to use wasn't working. I'm not sure exactly on the reason, but creating a new one seemed to solve the problem.

<sdk>/tools/mksdcard <size> <path>

<sdk>/emulator -avd <name> -sdcard <path.from.above>


来源:https://stackoverflow.com/questions/14987309/android-activityinstrumentationtestcase2-nullpointerexception-on-getting-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!