Android的ActivityInstrumentationTestCase2 NullPointerException异常的情况下获得 [英] Android ActivityInstrumentationTestCase2 NullPointerException on getting context

查看:775
本文介绍了Android的ActivityInstrumentationTestCase2 NullPointerException异常的情况下获得的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ActivityInstrumentationTestCase2和我有没有运气得到的上下文进行测试。

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();

我得到(第37行是context.getExternal线)

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

我也尝试了所有的下面同样的错误,即使getInstrumentation.waitForIdleSync()在少数地方(我在另一个线程看到的)为好。我试图创造新的MainActivity()对象,并从那里得到的上下文以及getActivity(),同样的问题。

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?

编辑1 - 这样看来,这个问题只发生在一个模拟器。如果我用一个真实的设备来运行测试他们都通过。

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.

推荐答案

有实际上是两个问题,我不得不解决的问题。第一个是,我不得不列入我的主要项目的外部库。当运行测试时,没有被正确地添加外部库。当创建主,这是在试图利用图书馆静静地失败。为了解决这个我也跟着不同的解决方案在这里:

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:

<一个href=\"http://stackoverflow.com/questions/2472059/cant-build-and-run-an-android-test-project-created-using-ant-create-test-proje\">Can't建立和运行一个Android测试项目中使用&QUOT创建的;蚂蚁创建测试项目&QUOT;测试时项目在libs目录罐子

这使我这个:

<一个href=\"http://stackoverflow.com/questions/8595006/android-unit-test-using-ant-with-library-project?lq=1\">Android使用带库项目蚂蚁单元测试

<一个href=\"http://stackoverflow.com/questions/10126358/android-unit-test-using-ant-with-library-project-r18\">Android用蚂蚁与库项目R18 单元测试

我定期但是略有不同。如果不使用这些项目的覆盖-compile标签,我只是复制的build.xml编译我的/工具/蚂蚁在我的测试中的build.xml并添加

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.

第二个问题是,SD卡模拟器试图用不工作。我不知道确切的原因,但创建一个新的似乎解决问题。

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>

这篇关于Android的ActivityInstrumentationTestCase2 NullPointerException异常的情况下获得的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆