如何将用户添加到角色

我正在尝试在注册函数中为用户分配一个角色。

通过使用下面的代码:

try {
    role.save(new CloudRoleCallback() {
    @Override
    public void done(CloudRole cloudRole, CloudException t) throws     CloudException {
        if(cloudRole != null){
            System.out.println("Adding user role"+ ""+   cloudRole.getRoleName());
            userObj.addToRole(role, new CloudUserCallback() {
                @Override
                public void done(CloudUser cloudUser, CloudException e) throws CloudException {
                    if(cloudUser != null){
                        System.out.println("role in role added successfully");
                        }
                    if(e != null){
                        e.printStackTrace();
                        }
                }
            });
        }
        if( t != null){
            t.printStackTrace();
                }
    }
});

} 
userObj.signUp(new CloudUserCallback() {
    @Override
    public void done(CloudUser cloudUser, CloudException e) throws  CloudException {
    if (cloudUser != null) {
        onSignupSuccess();
       }
    if (e != null) {
        e.printStackTrace();
        error = e.getMessage();
        error = error.substring(error.indexOf(""));
        onSignupFailed();
        progressDialog.dismiss();
        }
    }
});

当我尝试运行上面的代码时,我得到以下错误。

E / AndroidRuntime:致命异常:AsyncTask#2过程:信息,PID:967 java.lang.RuntimeException:在java中执行android.os.AsyncTask $ 3.done(AsyncTask.java:300)处的doInBackground()时发生错误。 util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)at java.util.concurrent.FutureTask.setException(FutureTask.java:222)at java.util.concurrent.FutureTask.run(FutureTask.java:242)at android .os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java: 587)at java.lang.Thread.run(Thread.java:818)导致:java.lang.NullPointerException:试图在Java的空对象引用上调用虚方法'int java.lang.String.length()' (JSONObject.java:324)at io.cloudboost.CloseUser 。添加 Role(CloudUser.java:317)at com.rakesh_kr.msrit_info.SignUp $ signupClass $ 1.done(SignUp.java:149)at i.cloudboost.CloudRole.save(CloudRole.java:83)at com.rakesh_kr.msrit_info。 SignUp $ signupClass.doInBackground(SignUp.java:144)at com.rakesh_kr.msrit_info.SignUp $ signupClass.doInBackground(SignUp.java:126)at android.os.AsyncTask $ 2.call(AsyncTask.java:288)at java。 util.concurrent.FutureTask.run(FutureTask.java:237)at android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587)at java.lang.Thread.run(Thread.java:818)


@Kr拉克什您的通话顺序是错误的,你基本上是呼吁addToRole上不存在的User ,因为你user.signUp在后面的顺序调用。
你应该做的是这样的:

  • 注册
  • done注册回叫的方法中创建并保存角色
  • 调用addToRole里面的role.save回调done方法
  • 以下是我建议的代码,所以你可以选择一片叶子:

                user.signUp(new CloudUserCallback() {
    
                @Override
                public void done(final CloudUser user1, CloudException e)
                        throws CloudException {
                    if (e != null)
                        e.printStackTrace();
                    else {
                        CloudRole role = new CloudRole(str);
                        role.save(new CloudRoleCallback() {
    
                            @Override
                            public void done(CloudRole x, CloudException t)
                                    throws CloudException {
                                if (t != null)
                                    t.printStackTrace();
                                else {
                                    user1.addToRole(x, new CloudUserCallback() {
    
                                        @Override
                                        public void done(CloudUser user,
                                                CloudException e)
                                                throws CloudException {
                                            if (e != null)
                                                e.printStackTrace();
                                            else {
                                                System.out
                                                    .println("user added to role");
                                            }
    
                                        }
                                });
                            }
    
                        }
                    });
                }
    
            }
        });
    
    链接地址: http://www.djcxy.com/p/78415.html

    上一篇: How to add user to a role

    下一篇: Error when using fragement and asynctask