解决百度地图在模拟器上运行报 java.lang.IllegalArgumentException: No config chosen问题

本文主要是介绍解决百度地图在模拟器上运行报 java.lang.IllegalArgumentException: No config chosen问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

解决百度地图在模拟器上运行报 java.lang.IllegalArgumentException: No config chosen 问题

1. 问题复现

在近期公司使用模拟器(网易MuMu)进行项目演示时,在进入存在百度地图(Android版本 7.4.2版本)之后,页面出现奔溃,后台日志为:

Back traces starts.
java.lang.IllegalArgumentException: No config chosen
com.baidu.platform.comapi.map.h$a.chooseConfig(GLTextureView.java:655)
com.baidu.platform.comapi.map.h$e.a(GLTextureView.java:789)
com.baidu.platform.comapi.map.h$f.l(GLTextureView.java:1164)
com.baidu.platform.comapi.map.h$f.run(GLTextureView.java:1002)

2. 查找源码,定位问题

经过问题的复盘,找到了是位于源码位置报错的:

com.baidu.platform.comapi.map.h$a.chooseConfig(GLTextureView.java:655)

具体代码位置如下:

com.baidu.platform.comapi.map.h

类下面的一个抽象类 a ,实现了EGLConfigChooser 接口,在实现chooseConfig接口时报错:

private abstract class a implements EGLConfigChooser {protected int[] a;public a(int[] configSpec) {this.a = this.a(configSpec);}public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {            // 代码省略... EGLConfig config = this.a(egl, display, configs);if (config == null) {throw new IllegalArgumentException("No config chosen");} else {return config;}}abstract EGLConfig a(EGL10 var1, EGLDisplay var2, EGLConfig[] var3);}

我们可以看到 EGLConfig这个类是由a方法返回的,我们可以看到,当config==null 时,会直接报出异常IllegalArgumentException,那么我们可以查看一下这个抽象类a是由谁来集成的?

通过搜索源码,它的继承方式是这样的:

package com.baidu.platform.comapi.map;public class h extends TextureView {private abstract class a implements EGLConfigChooser {}private class b extends com.baidu.platform.comapi.map.h.a {private int[] j = new int[1];protected int c;protected int d;protected int e;protected int f;protected int g;protected int h;public b(int redSize, int greenSize, int blueSize, int alphaSize, int depthSize, int stencilSize) {super(new int[]{12324, redSize, 12323, greenSize, 12322, blueSize, 12321, alphaSize, 12325, depthSize, 12326, stencilSize, 12344});this.c = redSize;this.d = greenSize;this.e = blueSize;this.f = alphaSize;this.g = depthSize;this.h = stencilSize;}public EGLConfig a(EGL10 egl, EGLDisplay display, EGLConfig[] configs) {EGLConfig[] var4 = configs;int var5 = configs.length;for(int var6 = 0; var6 < var5; ++var6) {EGLConfig config = var4[var6];int d = this.a(egl, display, config, 12325, 0);int s = this.a(egl, display, config, 12326, 0);if (d >= this.g && s >= this.h) {int r = this.a(egl, display, config, 12324, 0);int g = this.a(egl, display, config, 12323, 0);int b = this.a(egl, display, config, 12322, 0);int a = this.a(egl, display, config, 12321, 0);if (r == this.c && g == this.d && b == this.e && a == this.f) {return config;}}}return null;}private int a(EGL10 egl, EGLDisplay display, EGLConfig config, int attribute, int defaultValue) {return egl.eglGetConfigAttrib(display, config, attribute, this.j) ? this.j[0] : defaultValue;}}private class i extends com.baidu.platform.comapi.map.h.b {public i(boolean withDepthBuffer) {super(8, 8, 8, 0, withDepthBuffer ? 16 : 0, 0);}}}

综上所属,h下面的a,bi的关系为:

我们可以看到 h.c中其实没有a方法实现的,那么a方法的实现就是在h.b中了,我们可以来简单看一下h.b方法的实现:

        public EGLConfig a(EGL10 egl, EGLDisplay display, EGLConfig[] configs) {EGLConfig[] var4 = configs;int var5 = configs.length;for(int var6 = 0; var6 < var5; ++var6) {EGLConfig config = var4[var6];int d = this.a(egl, display, config, 12325, 0);int s = this.a(egl, display, config, 12326, 0);if (d >= this.g && s >= this.h) {int r = this.a(egl, display, config, 12324, 0);int g = this.a(egl, display, config, 12323, 0);int b = this.a(egl, display, config, 12322, 0);int a = this.a(egl, display, config, 12321, 0);if (r == this.c && g == this.d && b == this.e && a == this.f) {return config;}}}return null;}private int a(EGL10 egl, EGLDisplay display, EGLConfig config, int attribute, int defaultValue) {return egl.eglGetConfigAttrib(display, config, attribute, this.j) ? this.j[0] : defaultValue;}

这里我们可以看到,当执行r == this.c && g == this.d && b == this.e && a == this.f为真时,才能返回对应的config对象,如果都不匹配,那么将会导致返回null·,今儿导致程序奔溃。问题找到了,我们应该怎么去改它呢?

3. 通过源码去修复它

我也不拐弯抹角了,经过对源码的分析,列举一下自己对这个问题修复的看法。首先,我们知道了问题所在的地方,那么我们是否在方法类b中的a方法永远不返回null,那样就不会导致出现No config chosen异常,虽然这种方法会导致所选择的EGLConfig和所需要的config不匹配,导致页面存在拉伸或者压缩的问题,但是这相比于奔溃,应该会好很多。那么就按照这个说的干吧。

首先,要修改返回值,使用它原有的逻辑肯定是不行的,但是我们返现这个 h.a抽象类实现的是 EGLConfigChooser,它是来自android.opengl.GLSurfaceView下的一个接口,比较熟悉openGL的人应该比较熟悉它:

    public interface EGLConfigChooser {/*** Choose a configuration from the list. Implementors typically* implement this method by calling* {@link EGL10#eglChooseConfig} and iterating through the results. Please consult the* EGL specification available from The Khronos Group to learn how to call eglChooseConfig.* @param egl the EGL10 for the current display.* @param display the current display.* @return the chosen configuration.*/EGLConfig chooseConfig(EGL10 egl, EGLDisplay display);}

那我能不能自定义一下我们的h.ah.bh.i类呢,把源码拷一遍,然后把a方法返回值修改一下即可,好,那么说干就干:

h.a类如下:

public abstract class ParentEGLConfigChooser implements GLSurfaceView.EGLConfigChooser {protected int[] a;public ParentEGLConfigChooser(int[] configSpec) {this.a = this.a(configSpec);}public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {int[] num_config = new int[1];if (!egl.eglChooseConfig(display, this.a, (EGLConfig[])null, 0, num_config)) {throw new IllegalArgumentException("eglChooseConfig failed");} else {int numConfigs = num_config[0];if (numConfigs <= 0) {throw new IllegalArgumentException("No configs match configSpec");} else {EGLConfig[] configs = new EGLConfig[numConfigs];if (!egl.eglChooseConfig(display, this.a, configs, numConfigs, num_config)) {throw new IllegalArgumentException("eglChooseConfig#2 failed");} else {EGLConfig config = this.a(egl, display, configs);if (config == null) {throw new IllegalArgumentException("No config chosen");} else {return config;}}}}}abstract EGLConfig a(EGL10 var1, EGLDisplay var2, EGLConfig[] var3);private int[] a(int[] configSpec) {int len = configSpec.length;int[] newConfigSpec = new int[len + 2];System.arraycopy(configSpec, 0, newConfigSpec, 0, len - 1);newConfigSpec[len - 1] = 12352;newConfigSpec[len] = 4;newConfigSpec[len + 1] = 12344;return newConfigSpec;}}

对于h.b类,我们可以实现如下:

public class SubEGLConfigChooser extends ParentEGLConfigChooser {private final int[] j = new int[1];protected int c;protected int d;protected int e;protected int f;protected int g;protected int h;public SubEGLConfigChooser(int redSize, int greenSize, int blueSize, int alphaSize, int depthSize, int stencilSize) {super(new int[]{12324, redSize, 12323, greenSize, 12322, blueSize, 12321, alphaSize, 12325, depthSize, 12326, stencilSize, 12344});this.c = redSize;this.d = greenSize;this.e = blueSize;this.f = alphaSize;this.g = depthSize;this.h = stencilSize;}public EGLConfig a(EGL10 egl, EGLDisplay display, EGLConfig[] configs) {BaseLog.i("----show the a------>>" + egl + "---->>" + display + "---->>" + Arrays.toString(configs));for (EGLConfig config : configs) {int d = this.a(egl, display, config, 12325);int s = this.a(egl, display, config, 12326);if (d >= this.g && s >= this.h) {int r = this.a(egl, display, config, 12324);int g = this.a(egl, display, config, 12323);int b = this.a(egl, display, config, 12322);int a = this.a(egl, display, config, 12321);if (r == this.c && g == this.d && b == this.e && a == this.f) {return config;}}}//TODO 直接修改这里,返回第一个 configs[0], 暂时还未发现任何异常return configs[0];}private int a(EGL10 egl, EGLDisplay display, EGLConfig config, int attribute) {return egl.eglGetConfigAttrib(display, config, attribute, this.j) ? this.j[0] : 0;}
}

同样,对于h.i类,我们可以自定义类为:

public class TargetEGLConfigChooser extends SubEGLConfigChooser{public TargetEGLConfigChooser() {super(8, 8, 8, 0, 16 , 0);}
}

这三个类我们已经写好了,那么如何将我们的TargetEGLConfigChooser 替换成目标h.i方法呢?这个可能要花一些时间,我们来大致了解一下 BaiduMap的基础架构,我们以 TextureMapView为例子:

TextureMapView是继承自ViewGroup, 它有一个私有属性值b,其类型为MapTextureView, 属性bTextureMapView的初始化方法中被初始化:

MapTextureView中,首先它是继承自com.baidu.platform.comapi.map.h的:

同时,我们在h类中找到了h,h是一个EGLConfigChooser类型的接口,

在这里插入图片描述

通过程序分析, 那么这个h的实现类就是咋们的h.i. 主要问题分析完成了,那么就好做了,直接使用反射,将我们的h的实现类直接由h.i替换成我们的 TargetEGLConfigChooser即可,代码很简单,就几行:

public class TextureMapViewFix {public static void tryToFixException(TextureMapView mapView) {try {Field b = mapView.getClass().getDeclaredField("b");  // 找到bb.setAccessible(true);Object bObject = b.get(mapView);BaseLog.i("bObject = " + bObject);if (null == bObject) {BaseLog.i("bObject is null and return");return;}Field h = bObject.getClass().getSuperclass().getDeclaredField("h"); //找到其父类,然后查找子元素hh.setAccessible(true);Object aObject = h.get(bObject);BaseLog.i("aObject = " + aObject);h.set(bObject, new TargetEGLConfigChooser());  //替换成咋们自定义的目标类  TargetEGLConfigChooserObject aObject1 = h.get(bObject);BaseLog.i("aObject1 = " + aObject1);  //检查是否更新成功}catch (Exception e) {e.printStackTrace();}}
}

4. 总结

可能baidu地图的源码是混淆的,所以啃起来不是特别的顺利,还是耐着性子看完了,问题其实并不复杂,弄清楚逻辑就比较简单了,可能就是java的反射需要点功底,其它的都好说。如果有任何问题,可以add v:javainstalling,备注:baidu.

这篇关于解决百度地图在模拟器上运行报 java.lang.IllegalArgumentException: No config chosen问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/621039

相关文章

SpringBoot实现多环境配置文件切换

《SpringBoot实现多环境配置文件切换》这篇文章主要为大家详细介绍了如何使用SpringBoot实现多环境配置文件切换功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 示例代码结构2. pom文件3. application文件4. application-dev文

JavaScript实战:智能密码生成器开发指南

本文通过JavaScript实战开发智能密码生成器,详解如何运用crypto.getRandomValues实现加密级随机密码生成,包含多字符组合、安全强度可视化、易混淆字符排除等企业级功能。学习密码强度检测算法与信息熵计算原理,获取可直接嵌入项目的完整代码,提升Web应用的安全开发能力 目录

python3 pip终端出现错误解决的方法详解

《python3pip终端出现错误解决的方法详解》这篇文章主要为大家详细介绍了python3pip如果在终端出现错误该如何解决,文中的示例方法讲解详细,感兴趣的小伙伴可以跟随小编一起了解一下... 目录前言一、查看是否已安装pip二、查看是否添加至环境变量1.查看环境变量是http://www.cppcns

java对接第三方接口的三种实现方式

《java对接第三方接口的三种实现方式》:本文主要介绍java对接第三方接口的三种实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录HttpURLConnection调用方法CloseableHttpClient调用RestTemplate调用总结在日常工作

IDEA下"File is read-only"可能原因分析及"找不到或无法加载主类"的问题

《IDEA下Fileisread-only可能原因分析及找不到或无法加载主类的问题》:本文主要介绍IDEA下Fileisread-only可能原因分析及找不到或无法加载主类的问题,具有很好的参... 目录1.File is read-only”可能原因2.“找不到或无法加载主类”问题的解决总结1.File

idea中project的显示问题及解决

《idea中project的显示问题及解决》:本文主要介绍idea中project的显示问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录idea中project的显示问题清除配置重China编程新生成配置总结idea中project的显示问题新建空的pr

Spring 缓存在项目中的使用详解

《Spring缓存在项目中的使用详解》Spring缓存机制,Cache接口为缓存的组件规范定义,包扩缓存的各种操作(添加缓存、删除缓存、修改缓存等),本文给大家介绍Spring缓存在项目中的使用... 目录1.Spring 缓存机制介绍2.Spring 缓存用到的概念Ⅰ.两个接口Ⅱ.三个注解(方法层次)Ⅲ.

Spring Boot 整合 Redis 实现数据缓存案例详解

《SpringBoot整合Redis实现数据缓存案例详解》Springboot缓存,默认使用的是ConcurrentMap的方式来实现的,然而我们在项目中并不会这么使用,本文介绍SpringB... 目录1.添加 Maven 依赖2.配置Redis属性3.创建 redisCacheManager4.使用Sp

Spring Cache注解@Cacheable的九个属性详解

《SpringCache注解@Cacheable的九个属性详解》在@Cacheable注解的使用中,共有9个属性供我们来使用,这9个属性分别是:value、cacheNames、key、key... 目录1.value/cacheNames 属性2.key属性3.keyGeneratjavascriptor

redis在spring boot中异常退出的问题解决方案

《redis在springboot中异常退出的问题解决方案》:本文主要介绍redis在springboot中异常退出的问题解决方案,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴... 目录问题:解决 问题根源️ 解决方案1. 异步处理 + 提前ACK(关键步骤)2. 调整Redis消费者组