继承AVUser后调用AVUser.getCurrentUser报ClassCastException错误

匿名注册成功后,无法用AVUser.getCurrentUser(XXX.class)转换

class XXX extends AVUser {}

AVAnonymousUtils.logIn(new LogInCallback<AVUser>() {

    @Override
    public void done(AVUser arg0, AVException arg1) {
        if(arg1==null){
            AVUser.getCurrentUser(XXX.class)
        }
    }
});

java.lang.ClassCastException: com.avos.avoscloud.AVUser cannot be cast to XXX
at com.xxx.MainActivity$2.done(MainActivity.java:89)
at com.avos.avoscloud.LogInCallback.internalDone0(LogInCallback.java:42)
at com.avos.avoscloud.LogInCallback.internalDone0(LogInCallback.java:32)
at com.avos.avoscloud.AVCallback.internalDone(AVCallback.java:18)
at com.avos.avoscloud.AVAnonymousUtils$1.onSuccess(AVAnonymousUtils.java:66)
...

这是什么原因? 是不是匿名注册不支持扩展AVUser?

阅读 5.2k
1 个回答
  /**
   * This retrieves the currently logged in AVUser with a valid session, either from memory or disk
   * if necessary.
   * 
   * @param userClass subclass.
   * @return The currently logged in AVUser subclass instance.
   */
  @SuppressWarnings("unchecked")
  public static <T extends AVUser> T getCurrentUser(Class<T> userClass) 

匿名登录之后,SDK为你创建的currentUser是AVUser类型的对象,而不是你这里的XX extends AVUser
你可以使用下面这个方法,将currentUser改为子类对象。

AVAnonymousUtils.logIn(new LogInCallback<AVUser>() {

    @Override
    public void done(AVUser arg0, AVException arg1) {
        if(arg1==null){
            AVUser user = AVUser.getCurrentUser();
            XX x = new XX();
            /* 设置x的属性 */
            AVUser.changeCurrentUser(x, true);
        }
    }
});
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进