解决百度地图在模拟器上运行报 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集成easypoi导出word换行处理过程

《springboot集成easypoi导出word换行处理过程》SpringBoot集成Easypoi导出Word时,换行符n失效显示为空格,解决方法包括生成段落或替换模板中n为回车,同时需确... 目录项目场景问题描述解决方案第一种:生成段落的方式第二种:替换模板的情况,换行符替换成回车总结项目场景s

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

SpringBoot中@Value注入静态变量方式

《SpringBoot中@Value注入静态变量方式》SpringBoot中静态变量无法直接用@Value注入,需通过setter方法,@Value(${})从属性文件获取值,@Value(#{})用... 目录项目场景解决方案注解说明1、@Value("${}")使用示例2、@Value("#{}"php

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

线上Java OOM问题定位与解决方案超详细解析

《线上JavaOOM问题定位与解决方案超详细解析》OOM是JVM抛出的错误,表示内存分配失败,:本文主要介绍线上JavaOOM问题定位与解决方案的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录一、OOM问题核心认知1.1 OOM定义与技术定位1.2 OOM常见类型及技术特征二、OOM问题定位工具

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

C++右移运算符的一个小坑及解决

《C++右移运算符的一个小坑及解决》文章指出右移运算符处理负数时左侧补1导致死循环,与除法行为不同,强调需注意补码机制以正确统计二进制1的个数... 目录我遇到了这么一个www.chinasem.cn函数由此可以看到也很好理解总结我遇到了这么一个函数template<typename T>unsigned

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。