【问题标题】:"You need to use a Theme.Appcompat theme..." when testing ActionBarActivity, but I am“您需要使用 Theme.Appcompat 主题...”在测试 ActionBarActivity 时,但我是
【发布时间】: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


【解决方案1】:

我会尝试两件事:

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

【讨论】:

  • 遗憾的是,您的任何建议都不适合我(另外,在我问这个问题时,我在 onCreate 中没有 setTheme,但同时也尝试过)。跨度>
  • 我也有同样的问题。我的 Activity 类扩展了AppCompatActivity,当尝试使用ActivityUnitTestCase@RunWith(AndroidJUnit4.class) 对其进行测试时,我看到了错误:java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 我试图将清单文件更改为使用Theme.AppCompat,但它似乎没有任何影响在测试中。
【解决方案2】:

在 manifest.xml 中的 application 下添加 android:theme="@style/Theme.AppCompat"

【讨论】:

    猜你喜欢
    • 2016-04-01
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-15
    相关资源
    最近更新 更多