如何将用户添加到角色

时间:2016-04-04 21:20:39

标签: android cloudboost

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

使用以下代码:

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:执行时发生错误   doInBackground()                                                                              在android.os.AsyncTask $ 3.done(AsyncTask.java:300)                                                                              在   java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)                                                                              at java.util.concurrent.FutureTask.setException(FutureTask.java:222)                                                                              在java.util.concurrent.FutureTask.run(FutureTask.java:242)                                                                              在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)                                                                              在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)                                                                              在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587)                                                                              在java.lang.Thread.run(Thread.java:818)                                                                           引起:java.lang.NullPointerException:尝试调用虚拟   null对象引用上的方法'int java.lang.String.length()'                                                                              在java.io.StringReader。(StringReader.java:47)                                                                              在io.cloudboost.json.JSONTokener。(JSONTokener.java:85)                                                                              at io.cloudboost.json.JSONObject。(JSONObject.java:324)                                                                              at io.cloudboost.CloudUser.addToRole(CloudUser.java:317)                                                                              在com.rakesh_kr.msrit_info.SignUp $ signupClass $ 1.done(SignUp.java:149)                                                                              在io.cloudboost.CloudRole.save(CloudRole.java:83)                                                                              在   com.rakesh_kr.msrit_info.SignUp $ signupClass.doInBackground(SignUp.java:144)                                                                              在   com.rakesh_kr.msrit_info.SignUp $ signupClass.doInBackground(SignUp.java:126)                                                                              在android.os.AsyncTask $ 2.call(AsyncTask.java:288)                                                                              在java.util.concurrent.FutureTask.run(FutureTask.java:237)                                                                              在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)                                                                              在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)                                                                              在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587)                                                                              在java.lang.Thread.run(Thread.java:818)

1 个答案:

答案 0 :(得分:0)

@Kr Rakesh您的通话顺序错误,您实际上是在不存在的addToRole上调用User,因为您在序列中稍后会调用user.signUp。 你应该做的是这样:

  1. 注册
  2. 在注册回调的done方法中创建并保存角色
  3. addToRole回拨
  4. done方法中调用role.save

    以下是我建议的代码,所以你可以选择一片叶子:

                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");
                                            }
    
                                        }
                                });
                            }
    
                        }
                    });
                }
    
            }
        });