"你需要使用Theme.Appcompat主题..."测试ActionBarActivity的时候,但我 [英] "You need to use a Theme.Appcompat theme..." when testing ActionBarActivity, but I am

查看:155
本文介绍了"你需要使用Theme.Appcompat主题..."测试ActionBarActivity的时候,但我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题测试的应用程序,它通过在Eclipse的Andr​​oid JUnit测试使用ActionBarActivity从Android的支持-V7-appcompat时。 在模拟器或设备都运行时,似乎很好地工作。

我试图用一个模拟应用在<一个href="http://stackoverflow.com/questions/22364433/activityunittestcase-and-startactivity-with-actionbaractivity">ActivityUnitTestCase和startActivity与ActionBarActivity ,改变了父主题值-V11等在<一个建议href="http://stackoverflow.com/questions/18063395/actionbarcompat-java-lang-illegalstateexception-you-need-to-use-a-theme-appcom">ActionBarCompat: java.lang.IllegalStateException:你需要使用Theme.AppCompat ,但它仍然无法正常工作

<一个href="http://stackoverflow.com/questions/21814825/you-need-to-use-a-theme-appcompat-theme-or-descendant-with-this-activity">You需要使用Theme.AppCompat主题(或后代)本活动没有给出一个答案要么,ALS的人问的问题也未曾一个Theme.AppCompat在他的清单中指定(这是我做的),也没有他真的想延长ActionBarActivity(这是我做的)。他的解决办法是简单地扩大活动代替。

我是什么做错了吗?

这是错误我得到(故障跟踪从JUnit的窗口):

  java.lang.IllegalStateException:您需要使用Theme.AppCompat主题(或后代)与本次活动。
在android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:108)
在android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57)
在android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
在android.hello.HelloWorldActivity.onCreate(HelloWorldActivity.java:14)
在android.app.Activity.performCreate(Activity.java:5104)
在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
在android.test.ActivityUnitTestCase.startActivity(ActivityUnitTestCase.java:158)
在android.hello.test.HelloWorldActivityTest.setUp(HelloWorldActivityTest.java:26)
在android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
在android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
在android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
在android.app.Instrumentation $ InstrumentationThread.run(Instrumentation.java:1661)
 

HelloWorldActivity.java

 包android.hello;

进口android.support.v7.app.ActionBar;
进口android.support.v7.app.ActionBarActivity;
进口android.app.Activity;
进口android.os.Bundle;
进口android.widget.TextView;

公共类HelloWorldActivity扩展ActionBarActivity {
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        TextView的电视=(TextView中)findViewById(android.hello.R.id.tv);
        tv.setText(你好,Android的);

    }
}
 

HelloWorldApplication.java

 包android.hello;

进口android.app.Application;
进口android.util.Log;

公共类HelloWorldApplication扩展应用{
    @覆盖
    公共无效的onCreate(){
        super.onCreate();
        setTheme(R.style.Theme_AppCompat);
    }
}
 

您好世界清单:

  ...
&LT;活动
    机器人:名称=。HelloWorldActivity
    机器人:标签=@字符串/ APP_NAME
    机器人:主题=@风格/ Theme.AppCompat&GT;
    ...
&LT; /活性GT;
....
 

从测试包:

HelloWorldActivityTest.java

 包android.hello.test;

进口android.hello.HelloWorldActivity;
进口android.content.Intent;
进口android.test.ActivityUnitTestCase;
进口android.widget.TextView;

公共类HelloWorldActivityTest扩展ActivityUnitTestCase&LT; HelloWorldActivity&GT; {

    HelloWorldActivity helloWorldActivity;
    的TextView TextView的;

    公共HelloWorldActivityTest(){
        超(HelloWorldActivity.class);
    }

    @覆盖
    保护无效设置()抛出异常{
        super.setUp();

        //开始ScanMe的MainActivity
        startActivity(新意图(getInstrumentation()getTargetContext(),HelloWorldActivity.class),NULL,NULL);

        //参考ScanMe的MainActivity
        helloWorldActivity =(HelloWorldActivity)getActivity();

        //参考ScanMe的MainActivity的code输入文本编辑
        TextView的=(的TextView)helloWorldActivity.findViewById(android.hello.R.id.tv);
    }

    @覆盖
    保护无效tearDown的()抛出异常{
        super.tearDown();
    }

    公共无效测试preconditions()抛出异常{
        assertNotNull(TextView的);
    }

    公共无效testInput codeField(){
        。字符串实际= textView.getText()的toString();
        预计字符串=你好,机器人;
        的assertEquals(预期,实际值);
    }
}
 

解决方案

添加安卓主题=@风格/ Theme.AppCompat下您的manifest.xml应用

I have a problem when testing an app which uses ActionBarActivity from android-support-v7-appcompat via Android JUnit test in Eclipse. When running in an emulator or device everything seems to work fine.

I tried using a mock application as in ActivityUnitTestCase and startActivity with ActionBarActivity and changed the parent theme in values-v11 etc. as suggested in ActionBarCompat: java.lang.IllegalStateException: You need to use a Theme.AppCompat but it still does not work.

You need to use a Theme.AppCompat theme (or descendant) with this activity does not give an answer either, als the person asking the question neither had an Theme.AppCompat specified in his manifest (which I do), nor did he really want to extend ActionBarActivity (which I do). His solution was to simply extend Activity instead.

What am I doing wrong?

This is the error I get (Failure-Trace from the 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");

    }
}

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

From the test package:

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

解决方案

add android:theme="@style/Theme.AppCompat" under application in your manifest.xml

这篇关于&QUOT;你需要使用Theme.Appcompat主题...&QUOT;测试ActionBarActivity的时候,但我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆