gpt4 book ai didi

android - ActivityUnitTestCase 和 Google Maps Android API v2

转载 作者:行者123 更新时间:2023-11-29 18:03:22 24 4
gpt4 key购买 nike

我有一个 Android 项目,它使用 Google Maps Android API v2 (google-play-services_lib) 的外部库项目。

我还有测试项目,从第一点开始测试我的项目。

package third.task.android.test;

import third.task.android.CompassActivity;
import third.task.android.R;
import android.app.Activity;
import android.content.Intent;
import android.test.ActivityUnitTestCase;

public class CompassActivityTest extends ActivityUnitTestCase<CompassActivity> {

public CompassActivityTest() {
super(CompassActivity.class);
}

public void testTruth() {
assertTrue(true);
}

public void testPreConditions() {
startActivity(new Intent(getInstrumentation().getTargetContext(), CompassActivity.class), null, null);
Activity activity = getActivity();
assertNull(activity.findViewById(R.id.compasRadar));
}
}

这是我的失败轨迹,它实际上显示了有关加载 class="com.google.android.gms.maps.MapFragment"

的错误
android.view.InflateException: Binary XML file line #8: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:308)
at android.app.Activity.setContentView(Activity.java:1924)
at third.task.android.CompassActivity.onCreate(CompassActivity.java:55)
at android.app.Activity.performCreate(Activity.java:5206)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
at android.test.ActivityUnitTestCase.startActivity(ActivityUnitTestCase.java:158)
at third.task.android.test.CompassActivityTest.testPreConditions(CompassActivityTest.java:19)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:192)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:177)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1608)
Caused by: java.lang.IllegalStateException: Not on the main thread
at maps.am.r.b(Unknown Source)
at maps.ar.d.b(Unknown Source)
at maps.y.ae.setCompassEnabled(Unknown Source)
at maps.y.ae.a(Unknown Source)
at maps.y.ae.a(Unknown Source)
at maps.y.bu.a(Unknown Source)
at maps.y.p.onCreateView(Unknown Source)
at com.google.android.gms.maps.internal.IMapFragmentDelegate$Stub.onTransact(IMapFragmentDelegate.java:107)
at android.os.Binder.transact(Binder.java:326)
at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
at com.google.android.gms.maps.MapFragment$b.onCreateView(Unknown Source)
at com.google.android.gms.internal.d$4.a(Unknown Source)
at com.google.android.gms.internal.d.a(Unknown Source)
at com.google.android.gms.internal.d.onCreateView(Unknown Source)
at com.google.android.gms.maps.MapFragment.onCreateView(Unknown Source)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:807)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1013)
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1112)
at android.app.Activity.onCreateView(Activity.java:4857)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
... 24 more

当我删除负责在布局中显示 map 的代码以及依赖于 map 的代码 fragment 时,一切正常,但我必须使用包括 map 在内的全部功能测试我的应用程序。有什么办法吗?

最佳答案

ActivityInstrumentationTestCase2 怎么样?无需启动 Activity ,您可以简单地使用getActivity()。如果您需要使用自定义启动 Intent ,您可以使用 setUp() 中的 setActivityIntent(i) 指定它。我已经测试了它的工作:

package com.grt_team.airplanemode.test;

import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;

import com.grt_team.airplanemode.MainActivity;
import com.grt_team.airplanemode.R;

public class Test extends ActivityInstrumentationTestCase2<MainActivity> {

public Test() {
super(MainActivity.class);
}

public void testTruth() {
assertTrue(true);
}

@Override
protected void setUp() throws Exception {
super.setUp();
//setActivityIntent(i);
}

public void testPreConditions() {
Activity activity = getActivity();

assertNotNull(activity);
assertNull(activity.findViewById(0));
assertNotNull(activity.findViewById(R.id.map_fragment));
}

}

关于android - ActivityUnitTestCase 和 Google Maps Android API v2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14644269/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com