当前位置: 首页 > 知识库问答 >
问题:

“您需要使用一个主题。AppCompat主题...”当测试ActionBarActivity时,但我是

连时铭
2023-03-14

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

我做错了什么?

这是我得到的错误(从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");

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

共有1个答案

曹经业
2023-03-14

我会尝试两件事:

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