“您需要使用 Theme.Appcompat 主题...”当测试ActionBarActivity时,但我是
2022-09-01 05:28:54
在测试一个使用来自Android-support-v7-appcompat的ActionBarActivity的应用程序时,我遇到了一个问题,它通过Eclipse中的Android JUnit测试。在模拟器或设备中运行时,一切似乎都运行正常。
我尝试使用模拟应用程序,如在ActiveUnitTestCase中使用,并使用ActionBarActivity启动Activity,并按照ActionBarCompat:java.lang.IllegalStateException:您需要使用主题.AppCompat,但它仍然不起作用。
您需要使用此活动的主题.AppCompat主题(或后代)也不会给出答案,因此提出问题的人既没有在他的清单中指定主题.AppCompat(我这样做),也不是真的想扩展ActionBarActivity(我这样做)。他的解决方案是简单地扩展活动。
我做错了什么?
这是我得到的错误(来自 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);
}
}
你好世界宣言:
...
<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 );
}
}