【发布时间】:2014-07-01 04:21:10
【问题描述】:
在 Eclipse 中通过 Android JUnit 测试使用来自 android-support-v7-appcompat 的 ActionBarActivity 的应用程序时,我遇到了问题。 在模拟器或设备中运行时,一切似乎都正常。
我尝试使用 ActivityUnitTestCase and startActivity with ActionBarActivity 中的模拟应用程序,并按照 ActionBarCompat: java.lang.IllegalStateException: You need to use a Theme.AppCompat 中的建议更改了 values-v11 等中的父主题,但它仍然无法正常工作。
You need to use a Theme.AppCompat theme (or descendant) with this activity 也没有给出答案,因为提出问题的人既没有在他的清单中指定 Theme.AppCompat(我这样做),他也不想扩展 ActionBarActivity(我这样做)。他的解决方案是简单地扩展 Activity。
我做错了什么?
这是我得到的错误(来自 Junit 窗口的故障跟踪):
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 清单:
...
<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 );
}
}
【问题讨论】:
-
我也遇到了同样的问题
-
在
super.onCreate();之前设置主题当调用super时默认主题被加载,因此它会给你这个异常。 -
我在单元测试中也看到了这一点。我不认为这是一般问题的重复。我猜这是 MockContext 的问题。
-
使用 ActivityInstrumentationTestCase2 代替 ActivityUnitTestCase 可解决 JUnit 中的此错误。但是使用 ActivityInstrumentationTestCase2 进行简单的单元测试对我来说没有意义。截至今天(2015 年 4 月 24 日),似乎有 NO 解决方案来测试支持 v7-appcompat ActionBar 和 ActivityUnitTestCase 的活动!!
-
这个问题不是重复的。但@Raghu 是正确的。
标签: java android unit-testing android-actionbar android-actionbar-compat