How to add user to a role
I am trying to assign a role to a user in my signup function.
by using below code:
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();
}
}
});
When i try to run above code i am getting following error.
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2 Process: info, PID: 967 java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:300) at java.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) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference at java.io.StringReader.(StringReader.java:47) at io.cloudboost.json.JSONTokener.(JSONTokener.java:85) at io.cloudboost.json.JSONObject.(JSONObject.java:324) at io.cloudboost.CloudUser.addTo Role(CloudUser.java:317) at com.rakesh_kr.msrit_info.SignUp$signupClass$1.done(SignUp.java:149) at io.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 Rakesh the sequence of your calls is wrong, you are essentially calling addToRole
on a non existent User
since you user.signUp
is called later in the sequence.
What you should be doing is like so:
done
method of signup callback addToRole
inside done
method of role.save
callback Below is the code I propose, so you can pick a leaf:
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/78416.html
上一篇: 在android studio中找不到类
下一篇: 如何将用户添加到角色