首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Android上进行仪器测试?

如何在Android上进行仪器测试?
EN

Stack Overflow用户
提问于 2018-07-23 01:47:01
回答 1查看 0关注 0票数 0

在Android上进行仪器测试时我应该检查什么。我尝试了intumentationRegistry.getTargetContext()来获取上下文。但还是有错误。

这是我的Java类:

    public class MarkerSize {
  /**
   * Convert a SVG into a BitmapDescriptor for map marker
   *
   * @param context     The main Activity
   * @param vectorResId The ID of the SVG
   * @return The map marker as a BitmapDesriptor

   */

  public BitmapDescriptor bitmapDescriptorFromVector ( Context context, int vectorResId ) {
    try {
      Drawable vectorDrawable = ContextCompat.getDrawable(context, vectorResId);

      vectorDrawable.setBounds(0,
              0,
              vectorDrawable.getIntrinsicWidth(),
              vectorDrawable.getIntrinsicHeight());
      Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
              vectorDrawable.getIntrinsicHeight(),
              Bitmap.Config.ARGB_8888);
      Canvas canvas = new Canvas(bitmap);
      vectorDrawable.draw(canvas);
       BitmapDescriptor bp= BitmapDescriptorFactory.fromBitmap(bitmap);
      return bp;
    }catch ( Resources.NotFoundException e ){
      return null;
    }
  }

  /**
   * Change the size of the marker
   * @param context MapsActivity
   * @param iconName The name of the SVG file
   * @param width The desired width for the icon
   * @param height The desired heigh for the icon
   * @return A Bitmap representation of the SVG
   */
  public Bitmap resizeMapIcons (Context context, String iconName, int width, int height ) {
    Bitmap imageBitmap = BitmapFactory.decodeResource (context.getResources (),
                                                       context.getResources ().getIdentifier
                                                           (iconName,
                                                            "drawable",
                                                            context.getPackageName ()));
    Bitmap resizedBitmap = Bitmap.createScaledBitmap (imageBitmap, width, height, false);
    return resizedBitmap;
  }
}

这是我的简短测试:

@RunWith(JUnit4.class)
@SmallTest
public class testMarkersmethods {

  MapViewFragment mapViewFragment;
  private int VECTORID= R.drawable.ic_dest_pin;
  Context context=InstrumentationRegistry.getTargetContext();
  MarkerSize markerSize=new MarkerSize();
  BitmapDescriptor bitmapDescriptorrr ;

  @Test
  public void test_bitMapfromVector(){
    if(context!=null) {
      BitmapDescriptor bitmapDescriptor = markerSize.bitmapDescriptorFromVector(context, VECTORID);
      Assert.assertNotNull(bitmapDescriptor);
      Assert.assertEquals(bitmapDescriptorrr, bitmapDescriptor);
    }
  }
}

我要拿到这张日志:

java.lang.NullPointerException: IBitmapDescriptorFactory is not initialized
at com.google.android.gms.common.internal.Preconditions.checkNotNull(Unknown Source:8)
at com.google.android.gms.maps.model.BitmapDescriptorFactory.zzf(Unknown Source:4)
at com.google.android.gms.maps.model.BitmapDescriptorFactory.fromBitmap(Unknown Source:2)
at algorithm.MarkerSize.bitmapDescriptorFromVector(MarkerSize.java:38)
at com.example.giheok.streetview.testMarkersmethods.test_bitMapfromVector(testMarkersmethods.java:33)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2145)
EN

回答 1

Stack Overflow用户

发布于 2018-07-23 11:40:29

在错误的第1行中,我看到NullPointerException首先在方法之外初始化。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100005616

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档