Sahar Vanunu
Sahar Vanunu

Reputation: 454

android save object in firebase

I created sign in app and I am using with firebase database. But I have a problem when I am trying to save object in the database. Although when I am trying to save simple string or simple HashMap object everything working.

I got an error:

com.google.firebase.database.DatabaseException: Found a conflicting setters with name: setWallpaper (conflicts with setWallpaper defined on android.content.ContextWrapper)

Here is my code:

Main Activity:

public class MainActivity extends AppCompatActivity {
    EditText userName;
    EditText password;
    Button signInbtn;
    DatabaseReference url;
    Map<String, User> users = new HashMap<String, User>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final FirebaseDatabase database = FirebaseDatabase.getInstance();
        url = database.getInstance().getReference();
        userName = (EditText) findViewById(R.id.userName);
        password = (EditText) findViewById(R.id.password);
        signInbtn = (Button) findViewById(R.id.signInBtn);
        signInbtn.setOnClickListener(signObject);

    }  
    View.OnClickListener signObject = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            User user = new User("Moshe","1234");
            users.put("101",user);
            DatabaseReference userUrl = url.child("Users");
            userUrl.setValue(users);
            System.out.println(users);
        }
    };

Class User:

public class User extends Application{

    public static String password;
    public static String name ;

    public User(){

    }

    public User(String name,String password){
        this.name = name;
        this.password = password;
    }

    public  String getName() {
        return name;
    }

    public  void setName(String name) {
        User.name = name;
    }

    public  String getPassword() {
        return password;
    }

    public  void setPassword(String password) {
        User.password = password;
    }
}

I added permmision.Internet to the manifest. I read a lot of question about this subject and specially about Firebase, but I did not find any solution to my problem.

Upvotes: 4

Views: 2534

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

Firebase can only read and write fairly basic Java objects (so-called Plain Old Java Objects or POJOs) to the database. By extending your User class from Application, you're violating those rules.

The solution is to make your User class completely standalone:

public class User {
    ...

Upvotes: 4

Related Questions