SuperNotCalledException运行单元测试

时间:2011-09-19 18:12:42

标签: android

我正在尝试按照本教程::

运行此Android单元测试

http://developer.android.com/resources/tutorials/testing/helloandroid_test.html

并且这样做会得到 SuperNotCalledException

这是测试类代码::

package com.example.helloandroid2.test;

import android.test.ActivityInstrumentationTestCase2;
import android.widget.TextView;

import com.example.helloandroid2.HelloAndroid2Activity;

public class HelloAndroid2Test extends ActivityInstrumentationTestCase2<HelloAndroid2Activity>
{
    private HelloAndroid2Activity mActivity;
    private TextView mView;
    private String resourceString;

    public HelloAndroid2Test()
    {
        super("com.example.helloandroid2", HelloAndroid2Activity.class);
    }

    @Override
    protected void setUp() throws Exception
    {
        super.setUp();
        mActivity = this.getActivity();
        mView = (TextView) mActivity.findViewById(com.example.helloandroid2.R.id.textview);
        resourceString = mActivity.getString(com.example.helloandroid2.R.string.hello);
    }

    public void testPreconditions()
    {
          assertNotNull(mView);
    }

    public void testText()
    {
          assertEquals(resourceString,(String)mView.getText());
    }
}

我正在测试的课程

package com.example.helloandroid2;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HelloAndroid2Activity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.main);

    }
}

我已将项目API级别设置为2_3_1,同时使用avd set。 我在Windows Vista上运行带有ADT的Eclipse。

所有的智慧都得到了极大的回报。提前致谢。

克里斯

2 个答案:

答案 0 :(得分:4)

onCreate()中的HelloAndroid2Activity方法需要致电super.onCreate(savedInstanceState);

答案 1 :(得分:2)

public class HelloAndroid2Activity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    }
}