Google 的 Android HelloWorldTest 失败 - 在检索资源时接收到空指针
我正在使用 Google 的 HelloAndroidTest 教程:
http://developer.android.com /resources/tutorials/testing/helloandroid_test.html。
这是测试类:
package com.example.helloandroid.test;
import com.example.helloandroid.HelloAndroid;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.TextView;
public class HelloAndroidTest extends
ActivityInstrumentationTestCase2<HelloAndroid> {
private HelloAndroid mActivity;
private String resourceString;
private TextView mView;
public HelloAndroidTest() {
super("com.example.helloandroid", HelloAndroid.class);
}
protected void setUp(TextView mView) throws Exception {
super.setUp();
mActivity = this.getActivity();
mView = (TextView) mActivity
.findViewById(com.example.helloandroid.R.id.textview);
resourceString = mActivity
.getString(com.example.helloandroid.R.string.hello);
}
public void testPreconditions() {
assertNotNull(mView); // <== always null
//System.out.println("Resourse string: " + resourceString);
//assertNotNull(resourceString); // <== always null (when run)
}
public void testText() {
assertEquals(resourceString, (String) mView.getText());
}
}
这是 HelloAndroid 类:
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
这是 main.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/textview" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hello"/>
和 strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello Android!</string>
<string name="app_name">Hello, Android</string>
</resources>
mView 和资源字符串都未通过各自的 notNull 测试。
这是非常基本的,但它确实需要成功创建一个活动并从 HelloAndroid 项目中提取资源,这是我进行单元测试所需的功能。关于如何解决这个问题有什么想法吗?
I'm using the HelloAndroidTest tutorial from Google:
http://developer.android.com/resources/tutorials/testing/helloandroid_test.html.
Here's the test class:
package com.example.helloandroid.test;
import com.example.helloandroid.HelloAndroid;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.TextView;
public class HelloAndroidTest extends
ActivityInstrumentationTestCase2<HelloAndroid> {
private HelloAndroid mActivity;
private String resourceString;
private TextView mView;
public HelloAndroidTest() {
super("com.example.helloandroid", HelloAndroid.class);
}
protected void setUp(TextView mView) throws Exception {
super.setUp();
mActivity = this.getActivity();
mView = (TextView) mActivity
.findViewById(com.example.helloandroid.R.id.textview);
resourceString = mActivity
.getString(com.example.helloandroid.R.string.hello);
}
public void testPreconditions() {
assertNotNull(mView); // <== always null
//System.out.println("Resourse string: " + resourceString);
//assertNotNull(resourceString); // <== always null (when run)
}
public void testText() {
assertEquals(resourceString, (String) mView.getText());
}
}
Here's the HelloAndroid class:
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
This is main.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/textview" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hello"/>
And strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello Android!</string>
<string name="app_name">Hello, Android</string>
</resources>
Both mView and resource string fail their respective notNull tests.
This is pretty basic, but it does require an activity to be successfully created and the resource pulled from the HelloAndroid project, which is the functionality I need to get on with unit testing. Any ideas on how to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看您提交的第一个代码,我认为 setUp 不接受任何参数,因此带有参数的重写方法永远不会被调用,因此您的所有实例变量都为空。
Looking at your first code submission, I don't think setUp takes any parameters, therefore your overridden method with a parameter would never get called and therefore all your instance vars are null.
我想我已经明白了。看起来该活动需要在测试方法本身中创建。一旦我把它移到那里,它就工作得很好。 getActivity 文档实际上说明了一些与此相关的内容,这最终让我明白了。用不朽的 MLK 的话来说 - 终于免费了,终于免费了 - 终于免费了。
I think I've got it. It looks like the activity needs to be created in the test method itself. Once I moved it there, it works fine. The getActivity documentation actually states something to that effect, which is what finally clued me in. In the words of the immortal MLK - Free at last, free at last - free at last.