Support Topics Documentation Slack YouTube Blog

App crashes when registering new User


(Alexandre Coutinho) #1

So I’m trying to make a login and register menu, and when I try to use the code of the Documentation
“Backendless.UserService.register(user, new AsyncCallback<BackendlessUser>()”

The app just crashes when registering the new user, I’ve got an application class with the Backendless.initApp() on it, I’ve tried :

try{
Backendless.UserService.register(user);
}catch (BackendlessException exception){

                    }

and I always catch the exception, and I can’t use this line of code either “BackendlessFault fault = exception.getFault();”

Any help would be apprenciated!

Thanks,
Ninja.


(Mark Piller) #2

Hi Ninja,

The code you showed is a blocking API call. If you made that call on the main UI thread, Android will not like it, since blocking calls are not allowed. Take a look at the paragraph marked Important on the following page in the docs for more details:
https://backendless.com/docs/android/sync_and_async_calls.html

Regards,
Mark


(Alexandre Coutinho) #3

Hi Mark,

Thanks for answering! So I checked the page you linked me, and I’m still having some problems. First I’m not sure I fully understood if the blocking API call was my first try doing it or the second, but I can show you what I have right now.

So this is what happens when I press the “Register” button:

public void onClick(View v) {
if(etName.getText().toString().isEmpty() || etEmail.getText().toString().isEmpty() || etPassword.getText().toString().isEmpty() ||
etReEnter.getText().toString().isEmpty())
{
Toast.makeText(Register.this, “Please enter all details!”, Toast.LENGTH_SHORT).show();
}
else
{
if(etPassword.getText().toString().trim().equals(etReEnter.getText().toString().trim()))
{
String name = etName.getText().toString().trim();
String email = etEmail.getText().toString().trim();
String password = etPassword.getText().toString().trim();

//NEW USER FROM DATABASE
BackendlessUser user = new BackendlessUser();
user.setEmail(email);
user.setPassword(password);

showProgress(true);

new Thread(new Runnable(){

public void run(){
Backendless.UserService.register(user, new AsyncCallback<BackendlessUser>()
{
@Override
public void handleResponse(BackendlessUser response)
{
showProgress(false);
Toast.makeText(Register.this, “User sucessfully registered!”, Toast.LENGTH_SHORT).show();
Register.this.finish();
}

@Override
public void handleFault(BackendlessFault fault) {

Toast.makeText(Register.this, "Error: " + fault.getMessage(), Toast.LENGTH_SHORT).show();
showProgress(false);
}
});
}
}).start();

}
else
{
Toast.makeText(Register.this, “Please make sure your passwords match!”, Toast.LENGTH_SHORT).show();
}
}
}
});


(Mark Piller) #4

Now I see that you create a thread and then inside of that thread you’re using a non-blocking API. I think that’s excessive. Either use non-blocking API or create a thread and use blocking in there.

Also, do you have the Backendless.initApp call somewhere in your app? It should be done before you use any of the APIs.

Regards,
Mark


(Alexandre Coutinho) #5

Alright I get what you’re saying now, but how can I declare the user inside the thread?

new Thread(new Runnable() {
@Override
public void run() {
try{
Backendless.UserService.register(user);
}catch (BackendlessException exception){
Toast.makeText(Register.this, exception.getDetail(), Toast.LENGTH_LONG).show();
}
}
}).start();

Backendless.UserService.register(user); > it tells me that the variable user needs to be final…

Thanks a lot,
Ninja.


(Mark Piller) #6

With the code you showed earlier, you could change it to be like this:

final BackendlessUser user = new BackendlessUser();

(Mark Piller) #8

This explains the problem:

the SERVER_URL and the setUrl call are not needed. You can get rid of these two lines. I am curious, did you see a sample showing that somewhere?


(Mark Piller) #9

If you really want to have SERVER_URL, change the value of that constant to https://api.backendless.com


(Alexandre Coutinho) #10

I was using a tutorial and everything was working fine, until I tried to register the user. The guy on the video had this same code, and it worked for him.


(Alexandre Coutinho) #11

I’ve made the changes that you told me too, they did work! I just saved my first user, but the app still crashes…


(Mark Piller) #12

Well, the guy in the video is wrong :slight_smile: To make it right, please see my two previous responses.

If the app still crashes, use a debugger to figure out where the crash occurs.

Cheers,
Mark


(Alexandre Coutinho) #13

Thank you very much Mark! I just changed from the Blocking API to the non-Blocking one and everything is good now.

Thanks for the time man! Really appreciated!


(Mark Piller) #14

Glad you got it working!