当你testingActionBarActivity的时候,你需要使用Theme.Appcompat主题

我在使用Eclipse的Android JUnittestingtesting一个使用ActionBarActivity的应用程序时出现了问题,这个应用程序使用了android-support-v7-appcompat。 在模拟器或设备上运行时,似乎一切正常。

我尝试使用一个模拟应用程序在ActivityUnitTestCase和startActivity与ActionBarActivity和改变了值的母版主题-V11等,如在ActionBarCompatbuild议:java.lang.IllegalStateException:您需要使用一个Theme.AppCompat,但它仍然无法正常工作。

你需要使用一个Theme.AppCompat主题(或后代),这个活动也不会给出答案,也就是说,提问的人既没有在他的清单中指定的Theme.AppCompat(我也这么做),也没有真的想要扩展ActionBarActivity(我这样做)。 他的解决scheme是简单地扩展Activity。

我究竟做错了什么?

这是我得到的错误(Junit窗口中的Failure-Trace):

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> .... 

从testing包:

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

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

有两件事我会尝试:

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