"您需要使用Theme.Appcompat主题......"在测试ActionBarActivity时,我是

时间:2014-05-13 13:07:36

标签: java android unit-testing android-actionbar android-actionbar-compat

我在测试一个应用程序时遇到问题,该应用程序在Eclipse中通过Android JUnit测试使用android-support-v7-appcompat中的ActionBarActivity。 在模拟器或设备中运行时,一切似乎都能正常工作。

我尝试使用ActivityUnitTestCase and startActivity with ActionBarActivity中的模拟应用程序,并按照ActionBarCompat: java.lang.IllegalStateException: You need to use a Theme.AppCompat中的建议更改了值-v11等中的父主题,但它仍然不起作用。

You need to use a Theme.AppCompat theme (or descendant) with this activity也没有给出答案,也就是提问的人既没有在他的清单中指定的Theme.AppCompat(我这样做),也没有真正想要扩展ActionBarActivity(我这样做) 。他的解决方案是简单地扩展Activity。

我做错了什么?

这是我得到的错误(来自Junit-Window的失败跟踪):

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:108)
at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
at android.hello.HelloWorldActivity.onCreate(HelloWorldActivity.java:14)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.test.ActivityUnitTestCase.startActivity(ActivityUnitTestCase.java:158)
at android.hello.test.HelloWorldActivityTest.setUp(HelloWorldActivityTest.java:26)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661)

HelloWorldActivity.java

package android.hello;

import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HelloWorldActivity extends ActionBarActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView tv = (TextView) findViewById(android.hello.R.id.tv);
        tv.setText("Hello, Android");

    }
}

HelloWorldApplication.java

package android.hello;

import android.app.Application;
import android.util.Log;

public class HelloWorldApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        setTheme(R.style.Theme_AppCompat);
    }
}

Hello World Manifest:

...
<activity
    android:name=".HelloWorldActivity"
    android:label="@string/app_name" 
    android:theme="@style/Theme.AppCompat">
    ...
</activity>
....

从测试包中:

HelloWorldActivityTest.java

package android.hello.test;

import android.hello.HelloWorldActivity;
import android.content.Intent;
import android.test.ActivityUnitTestCase;
import android.widget.TextView;

public class HelloWorldActivityTest extends ActivityUnitTestCase<HelloWorldActivity> {

    HelloWorldActivity helloWorldActivity; 
    TextView textView;

    public HelloWorldActivityTest() {
        super(HelloWorldActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        // Starts the MainActivity of ScanMe
        startActivity(new Intent(getInstrumentation().getTargetContext(),       HelloWorldActivity.class), null, null);

        // Reference to the MainActivity of ScanMe
        helloWorldActivity = (HelloWorldActivity)getActivity();

        // Reference to the code input-TextEdit of the MainActivity of ScanMe
        textView = (TextView) helloWorldActivity.findViewById(android.hello.R.id.tv);
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public void testPreconditions() throws Exception {
        assertNotNull(textView);
    }

    public void testInputCodeField(){
        String actual=textView.getText().toString();
        String expected = "Hello, Android";
        assertEquals(expected,actual );
    }
}

2 个答案:

答案 0 :(得分:2)

在manifest.xml中的应用程序下添加android:theme="@style/Theme.AppCompat"

答案 1 :(得分:1)

我要尝试两件事:

  • 从onCreate中移除setTheme,它与清单是多余的,可能会导致混淆
  • 在清单
  • 中将主题设置为应用程序而不是活动级别