记录华为游戏SDK,ResourceLoaderUtil.getStringId() 空指针问题

闪退信息

08-14 21:01:07.111 17548 17548 E AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
408-14 21:01:07.111 17548 17548 E AndroidRuntime: at com.huawei.hms.utils.ResourceLoaderUtil.getStringId(ResourceLoaderUtil.java:1)
508-14 21:01:07.111 17548 17548 E AndroidRuntime: at com.huawei.hms.jos.games.player.AntiAddictionService$c.run(Unknown Source:33)
608-14 21:01:07.111 17548 17548 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:809)
708-14 21:01:07.111 17548 17548 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
808-14 21:01:07.111 17548 17548 E AndroidRuntime: at android.os.Looper.loop(Looper.java:166)
908-14 21:01:07.111 17548 17548 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:7555)
1008-14 21:01:07.111 17548 17548 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
1108-14 21:01:07.111 17548 17548 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:469)
1208-14 21:01:07.111 17548 17548 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:963)

如何复现

接入华为游戏sdk版本为 6.1.0.301

 implementation 'com.huawei.hms:game:6.1.0.301'

华为测试机,准备一个未成年实名账号,进入游戏,登录成功后,弹出防沉迷提示框,点击"知道了",游戏闪退。

如何处理

初始化华为SDK时候,添加代码 :

R e s o u r c e L o a d e r U t i l . s e t m C o n t e x t ( t h i s ) ; / / 设置防沉迷提示语的 C o n t e x t ,此行必须添加 \color{red}{ ResourceLoaderUtil.setmContext(this); // 设置防沉迷提示语的Context,此行必须添加} ResourceLoaderUtil.setmContext(this);//设置防沉迷提示语的Context,此行必须添加

详见华为开发者文档

public void init() {
  AccountAuthParams params = AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM_GAME;
  JosAppsClient appsClient = JosApps.getJosAppsClient(this);
  Task initTask;

  ResourceLoaderUtil.setmContext(this);  // 设置防沉迷提示语的Context,此行必须添加
  initTask = appsClient.init(
          new AppParams(params, new AntiAddictionCallback() {
              @Override
              public void onExit() {
              }
          }));
  initTask.addOnSuccessListener(new OnSuccessListener() {
      @Override
      public void onSuccess(Void aVoid) {
          showLog("init success");
          hasInit = true;
          Games.getBuoyClient(this).showFloatWindow();
      }
  }).addOnFailureListener(
          new OnFailureListener() {
              @Override
              public void onFailure(Exception e) {
                  if (e instanceof ApiException) {
                      ApiException apiException = (ApiException) e;
                      int statusCode = apiException.getStatusCode();
                      if (statusCode == JosStatusCodes.JOS_PRIVACY_PROTOCOL_REJECTED) { // 错误码为7401时表示用户未同意华为联运隐私协议
                          Log.i(TAG, "has reject the protocol");
                        
                      } else if (statusCode == GamesStatusCodes.GAME_STATE_NETWORK_ERROR) { // 错误码7002表示网络异常
                          Log.i(TAG, "Network error");
                    
                      } else if (statusCode == 907135003) {
                     
                          showLog("init statusCode=" + statusCode);
                          init();
                      } else {
                       
                      }
                  }
              }
          });
}

你可能感兴趣的:(华为,游戏,android,sdk)