本文介绍了获得空指针异常时,单元测试的Andr​​oid TabActivity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前从单元测试样品TabLayout 。这样的单元测试时,我得到一个NullPointerException异常:

I am currently unittesting the sample TabLayout from android website. I am getting a NullPointerException when unittesting it like this:

public class MainActivityUnitTest extends ActivityUnitTestCase<MainActivity>{

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

    @MediumTest
    public void testStartActivity(){
        setActivity(startActivity(new Intent(Intent.ACTION_MAIN), null, null));
    }
}

这是我收到的异常:

java.lang.NullPointerException
at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:277)
at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
at android.widget.TabHost.setCurrentTab(TabHost.java:326)
at android.widget.TabHost.addTab(TabHost.java:216)
at com.foo.android.MainActivity.onCreate(MainActivity.java:55)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.test.ActivityUnitTestCase.startActivity(ActivityUnitTestCase.java:159)
at com.foo.android.test.MainActivityUnitTest.testStartActivity(MainActivityUnitTest.java:17)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)

我试过,没有运气搜索网络。我希望有人已经解决之前,这样的问题。我做了什么错在这里?非常感谢你提前为您的帮助。

I tried to search the net with no luck. I am hoping somebody has resolved an issue like this before. What have I done wrong here? Thank you very much in advance for your assistance.

推荐答案

问题是的 ActivityUnitTestCase 的,因为它进行测试只在单位动作条是不是在单位,如果你想测试它,你必须扩展 ActivityInstrumentationTestCase2 并检查什么说mathume

The problem is ActivityUnitTestCase because it make a test only on "the unit" actionbar isn't in the unit if you want to test it you must extend ActivityInstrumentationTestCase2 and check what say mathume

这篇关于获得空指针异常时,单元测试的Andr​​oid TabActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 12:03