小编Lig*_*ght的帖子

当RoSlectric在Service实例中调用getSystemService时的NPE

当我试图在Service实例中调用getSystemService时,它抛出了一个NPE.它在onCreate中调用:

Vibrator  vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
Run Code Online (Sandbox Code Playgroud)

我创建了这样的Service实例:

@Test
public void test() throws Exception{
    FooService service = new FooService();
    service.oncreate();//NPE in this line
    //... intent declaration
    service.onStartCommand(intent, 0, 1);
}
Run Code Online (Sandbox Code Playgroud)

但是当我尝试将我的原始代码从服务实例itlef调用的getSystemService修改为xxApplication.getSystemService(XXX)时,它被应用程序调用,它没有抛出任何异常.那么,如何在不修改原始代码的情况下正确测试服务?

testing android robolectric

3
推荐指数
1
解决办法
780
查看次数

在ViewPager中旋转后,选项卡内容消失

我将FragmentTabHost放入ViewPager,但是在旋转设备后标签内容已经消失.如果我单击另一个选项卡以刷新内容,则选项卡内容将返回.我的代码有什么问题吗?请帮帮我.

public class MyActivity extends FragmentActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LayoutInflater inflater = getLayoutInflater();

        View tabView = inflater.inflate(R.layout.tab, null);
        FragmentTabHost tabHost = (FragmentTabHost) tabView.findViewById(android.R.id.tabhost);
        tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab1"), TabContent1Fragment.class, null);
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Tab2"), TabContent2Fragment.class, null);

        View page2 = inflater.inflate(R.layout.page2, null);
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
        List<View> list = new ArrayList<View>();
        list.add(page2);
        list.add(tabView);

        viewPager.setAdapter(new DemoPagerAdapter(list));
    }
}


public class TabContent1Fragment extends Fragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return inflater.inflate(R.layout.tab_content_1, null);
    }
} …
Run Code Online (Sandbox Code Playgroud)

android android-tabhost android-viewpager

1
推荐指数
1
解决办法
2177
查看次数